I have tried to use a lambda expression that with a reference parameter of an array position but I get errors, the error is in the item variable.
delegate void accion(ref int x);
static void Main(string [] Args)
{
int[][] m = new int[3][];
m[0] = new int[2];
m[1] = new int[5];
m[2] = new int[10];
accion ac = new accion(Ac);
recorrer(m,item => item = new Random().Next(0,1000) );
recorrer(m,item => Console.WriteLine(item));
}
public static void recorrer(int [][] m, accion ac)
{
for (int i = 0; i < m.Length; i++)
{
for (int j = 0; j < m[i].Length; j++)
{
ac(ref m[i][j]);
}
}
}
the error specifically says that the parameter must be declared with the word ref, I put it and I got another error that the variable must be assignable, then I better did them in methods for the delegate and there it works
public static void Ac(ref int item)
{
Console.WriteLine(item);
}
public static void Ac(ref int item)
{
item = new Random().Next(0,1000);
}
You must explicitly declare the type of the variable: