To see what's going on, change your lambda to maps.Add(obj => i);
.
With that change result
will be 3
, and that's why you're getting IndexOutOfBoundException
exception: you're trying to get myInstance[3]
which does not exist.
To make it work, add local int
variable within your loop and use that one as index instead of loop counter i
:
for (int i = 0; i < myInstance.Values.Length; i++)
{
int j = i;
maps.Add(obj => obj.Values[j]);
}