why doesn’t the element get swapped
public static void SwapArray(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(0); j++)
{
int temp = arr[i, j];
arr[i, j] = arr[j, i];
arr[j, i] = temp;
}
}
}
even if the parameter is without a ref modifier the array doesn’t change.
a copy of the reference is passed as a parameter right?
There is an error in your algorithm. For every i and j, your loop swaps
arr[i,j]andarr[j,i]twice.For example
arr[3,1]gets swapped witharr[1,3]once for i=3, j=1 and once for i=1, j=3. So the result is the original matrix. You should change the j-loop to