I experienced a problem today I cannot explain. I have a task which invokes a Method, but the integer parameter doesn't seem to be treathed like a value type.
I could reproduce it in a simple setting:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Start();
}
private static void Start()
{
int numberThreads = 3;
for (int i = 0; i < numberThreads; i++)
{
if (i == 3)
{
// Never gets hit
System.Diagnostics.Debugger.Break();
}
Task.Run(() => DoWork(i));
}
}
private static void DoWork(int index)
{
if (index == 3)
{
// index = 3
System.Diagnostics.Debugger.Break();
}
}
}
}
The (i == 3) in Start() never validates true, the (index == 3) in DoWork() is always true. How is this possible?