I have some code,
int count = 0;
list.ForEach(i => i.SomeFunction(count++));
This seems to not increment count. Is count passed by value here? Is there any difference if I use the {} in the lambda?
int count = 0;
list.ForEach(i =>
{
i.SomeFunction(count++);
});
Update 1
Sorry, my mistake, it does update the original count.
countis an int, and ints are value types, which means they are indeed passed by value. There is no semantic difference between your first and second example.(That said, it looks to me like it should be incrementing
count, since it should be capturing the original reference as far as the closure. To clarify — although count will be passed by value down into SomeFunction, things don’t get “passed” into your lambda expression when you use them inside the expression — they are the same reference as the external variable.)