I'm trying to understand why, in the following example, the value of 'fac' is the value 2, even after there's an assignment to the value 3.
public class Program
{
public static void Main()
{
int fac = 1;
Func<int, int> mul = (n) =>
{
fac = 2;
return fac * fac;
};
fac = 3;
Console.WriteLine(mul(fac));
Console.WriteLine(fac);
}
}
result:
4
2
I know that lambda expressions can themselves update captured variables (in this case 'fac') but to this extent seems confusing.