+ 2
[C#] Why the people saying that the arrays in C# are referent types ?
Hi. Why the people are saying that the arrays in C# are referent types ? Can you give me an example where the arrays are acting like a references ?
1 Respuesta
+ 6
in this example I create an array with a value of 5. I then pass the array to the other function that will change the value. Will it change the value of the array in the main method?
Yes, because its passing the reference of the array in the parameters. It's not passing the value, otherwise the function would have used a different array with the same values.
The Example/
static void main(String[] a){
int[] array = {5};
function(array);
Console.Write(array[0]);
}
static void function(int[] arr){
arr[0] = 100;
}
Array before calling function:
{5}
Array after calling function:
{100}
Output: 100