Wednesday, 15 June 2011

arrays - C# assigning variables -


I have something that bothers me, it would be great if any of you could explain this to me maybe this question I was asked before, but I am really out of ideas how to give it its name here is the problem:

  array1 = {1,2,3,4,5}; Array2 = array1; Array 1 [0] = 10 console. Light line (array 2 [0]); // - It says "10" in addition to "1"  

and when I use normal variables instead of arrays, like:

  Int A = 5; Int b = a; A = 10; Console.WriteLine (b); // - This will have the value of "5" instead of 10.  

I know how to copy Arri with values, I am just curious why this happens.

variable array 1 does not have a value of 1 or 'c', instead it keeps that address The place that points in the memory where the data is stored.

Then array2 = array1 was just giving the same address as array1 to array1 ... they both point at the same location.

If you want to allocate a new part of memory, you must declare an new array: int [] array2 = new int [5]; It reserves 5 * 32 bits in memory for your new array and gives array2 to the first bit address.


No comments:

Post a Comment