Say I have a function:
static void foo(int[,] arr, arg1)
{
//say arg1 = row - 1
for(int row = 0; row < arr.GetLength(0); row++)
{
for(int col = 0; col < arr.GetLength(1); col++)
{
MessageBox.Show(arr[arg1 , col]) // should be equal to arr[row-1,col]
}
}
}
foo(arr, "row-1"); // should be equal to arr[row-1,col]
I want to reference the loop variable with arg1.
Is this possible?
What type should arg1 be?
How do i write this?
You would do this by passing
Func<int,int>to the function:Then call it with a lamda which represents the transformation you want to do.
The problem with this approach is that the first iteration round
rowit will have the value0. By subtracting 1 from this you try to getarr[-1,col]which is outside of the bounds of the array. Im not sure what you’re trying to actually achieve, so I can only guess that either you want to start therowvariable at1:or perhaps a more complex rule for the lambda: