Skip to content

Redundant variable usage #4075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
// <Snippet2>
// <Snippet2>
using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
var rnd = new Random();
int breakIndex = rnd.Next(1, 11);
Nullable<long> lowest = new Nullable<long>();
public static void Main()
{
var rnd = new Random();
int breakIndex = rnd.Next(1, 11);

Console.WriteLine("Will call Break at iteration {0}\n",
breakIndex);
Console.WriteLine($"Will call Break at iteration {breakIndex}\n");

var result = Parallel.For(1, 101, (i, state) => {
Console.WriteLine("Beginning iteration {0}", i);
int delay;
Monitor.Enter(rnd);
delay = rnd.Next(1, 1001);
Monitor.Exit(rnd);
Thread.Sleep(delay);

if (state.ShouldExitCurrentIteration) {
if (state.LowestBreakIteration < i)
return;
}
var result = Parallel.For(1, 101, (i, state) =>
{
Console.WriteLine($"Beginning iteration {i}");
int delay;
lock (rnd)
delay = rnd.Next(1, 1001);
Thread.Sleep(delay);

if (i == breakIndex) {
Console.WriteLine("Break in iteration {0}", i);
state.Break();
if (state.LowestBreakIteration.HasValue)
if (lowest < state.LowestBreakIteration)
lowest = state.LowestBreakIteration;
else
lowest = state.LowestBreakIteration;
}
if (state.ShouldExitCurrentIteration)
{
if (state.LowestBreakIteration < i)
return;
}

Console.WriteLine("Completed iteration {0}", i);
});
if (lowest.HasValue)
Console.WriteLine("\nLowest Break Iteration: {0}", lowest);
else
Console.WriteLine("\nNo lowest break iteration.");
}
if (i == breakIndex)
{
Console.WriteLine($"Break in iteration {i}");
state.Break();
}

Console.WriteLine($"Completed iteration {i}");
});

if (result.LowestBreakIteration.HasValue)
Console.WriteLine($"\nLowest Break Iteration: {result.LowestBreakIteration}");
else
Console.WriteLine($"\nNo lowest break iteration.");
}
}
// The example displays output like the following:
// Will call Break at iteration 8
Expand Down