I know there are very similar questions but I need to know what's going on in this specific case. I wrote a function to reverse items of a multidimensional array along a specific dimension. To do this I convert the multidimensional array to a jagged array and than recursively find the array dimension where perform the reversal.
public static void Reverse(this Array array, int dimension, int start, int end)
{
object[] jag = array.ToJaggedArray();
if (!jag.ReverseAux(0, dimension, start, end))
throw new ArgumentOutOfRangeException("dimension", dimension, "Dimension must be nonnegative and less than array dimensions number.");
array = jag;
}
Now my problem is that when I run this method my input array has not been modified. Shouldn't an array be passed by reference? When I assign array = jag why it is correctly assigned inside the method but not outside?
For example:
// create 2D array of strings
string[,] ar = {{"00", "01", "02"},
{"10", "11", "12"},
{"20", "21", "22"}};
// reverse the first dimension (rows)
ar.Reverse(0, 0, 2);
// show the result
MessageBox.Show(ar.ToString(null, null));
The output is {{00, 01, 02}, {10, 11, 12}, {20, 21, 22}} that is exactly the original array. If I check the output in Reverse method
public static void Reverse(this Array array, int dimension, int start, int end)
{
//...
array = jag;
System.Windows.Forms.MessageBox.Show(array.ToString(null, null));
}
this shows the correct reversed array {{20, 21, 22}, {10, 11, 12}, {00, 01, 02}}.
Shouldn't an array be passed by reference? Why after the local assignment my input array is referencing the orignal pointer and not the assigned? Should I use ref keyword for array argument? If yes is it possible to continue using this method as an extension of Array type?
reforoutmodifiers. Extension methods don't support passing by reference however.inmodiier to pass by reference added in 7.3