I have the following, which runs at the initialization of a node graph system for every ports of a node;
private void Initialize()
{
// stats.Length return 3;
for (int i = 0; i < stats.Length; i++)
stats[i].Initialize(this, () => GetStats(i));
}
private void GetStats(int index)
{
// Issue; index returns 3
}
If I print the i
in the for loop, I get 0, 1, 2
as expected when I have 3 items.
Somehow, if I print index
in GetStats
, I get 3
.
I'm just at lost here, how is a value-type parameter in a method nested in a lambda can change value? Am I doing something stupid?