I am trying to understand how arrays work and ran into a problem. I have understood that arrays are a fixed data type and once defined cannot go out of it's bounds. However, the below method seems to be working fine and I don't understand why. Do I have an incomplete understanding of arrays
private static int[] return_Array()
{
int[] arr = new int[5];
arr = [1,2,3,4,5,6];
return arr;
}
Even if I did
arr = new int[] {1,2,3,4,5,6,7};
it's working without any error
Shouldn't I get an Index was outside the bounds of the array error in both scenarios?
You are not adding values to the array, you are replacing it.
int[] arr = new int[5]; --> int[] arr = [0,0,0,0,0]
arr = [1,2,3,4,5,6]; --> int[] arr = new int[] {1,2,3,4,5,6}