I'm trying to understand exactly what's happening in this code below:
static void someMethod(int[] a) {
int[] b = new int[5];
a = b;
}
static void Main(string[] args) {
int[] arr = new int[10];
someMethod(arr);
Console.WriteLine(arr.Length);
}
The thing I can't explain is why the Console.Writeline()
prints 10 and not 5. Arrays in C# are reference types, so I would've thought the line a = b
sets the variable a
to point to an array of int[5]
. Yet the Console.Writeline(
) prints 10. I'm at a loss to explain this value-type-like behavior of arrays. Can someone explain this?