I wanted to initialize an array of functions from a double Convert(int index, int input)
function where I pass the index of the element in the array like this:
var myfunctions= new Func<ushort, double>[10];
for (int i=0; i<10; i++)
{
myfunctions[i] = new Func<ushort, double>(x_in => {return Convert(i, in);});
}
but that doesn't work since any call to an element x of myfunctions array myfunctions[x](myVal)
will do this equivalent call Convert(10, myVal)
and x is always = 10 (last value of i in the initialisation loop)
So I had to code this ugly thing to make it work :
var myfunctions= new Func<ushort, double>[10];
myfunctions[0] = new Func<ushort, double>(x_in => {return Convert(0, in);});
myfunctions[1] = new Func<ushort, double>(x_in => {return Convert(1, in);});
...
myfunctions[9] = new Func<ushort, double>(x_in => {return Convert(9, in);});
Any suggestions ? Thanks