That should increment, proof:
class Foo
{
public void SomeFunction(int i) { }
}
static void Main()
{
int count = 0;
List<Foo> list = new List<Foo> {new Foo()};
list.ForEach(i => i.SomeFunction(count++));
Console.WriteLine(count); // 1
}
The lambda acts (as already stated) to "capture" count, essentially making the code like:
class Foo
{
public void SomeFunction(int i) { }
}
class CaptureScope
{
public int count;
public void AnonMethod(Foo foo)
{
foo.SomeFunction(count++);
}
}
static void Main()
{
CaptureScope scope = new CaptureScope();
scope.count = 0;
List<Foo> list = new List<Foo> {new Foo()};
list.ForEach(scope.AnonMethod);
Console.WriteLine(scope.count); // 1
}
The above is a rough approximation of how the compiler interprets a delegate lambda. As you can see, the count
is a field on a class, and is incremented.