An array in C # is a reference type, why do they act as value types?

According to MSDN, if all arrays are a reference type, then why in this code example the new value of t2 does not reflect the change in t1 ?

 string[] data = new[] { "One", "Two" }; var t1 = data[0]; Console.WriteLine(t1); var t2 = t1; t2 = "Three"; //assigning the new value, and this should reflect in t1 Console.WriteLine(t2); Console.WriteLine(t1); // this should print 'Three', but it prints 'One' Console.Read(); 

enter image description here

http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

Arrays are mechanisms that allow you to process multiple elements as a single collection. The standard Microsoft® .NET runtime (CLR) supports one-dimensional arrays, multidimensional arrays, and jagged arrays (array arrays). All types of arrays are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference types that are allocated in the managed heap, and your app variable contains a reference to the array, not the array.

+7
arrays c #
source share
6 answers

One photograph is worth a thousand words, this is what happens:

Before and after

The effect of assigning "Three" to t2 is that before assigning t1 and t2 reference is to the same object, but after the assignment, they refer to different objects. Nothing happens here.

The situation would be different if you had an array of mutable objects, and they used their value instead of setting their links. For example, imagine replacing an array of strings with an array of StringBuilder objects and calling t2.Replace("Two", "Three") instead of assigning it. Now the effect will be different, because t1 , t2 and data[0] will point to the same object.

+18
source share

Your question has nothing to do with arrays. Since your data[0] is string - and this is a reference type - this is the value of One , you never change its value. You just created a link to another line t2 and set a point with the same object using the link t1 . After you change this reference object to "Three" , but that will not affect what t1 means.

Look at your code line by line;

 var t1 = data[0]; 

Using this line, you created a link to the line as t1 , and this points to the "One" object.

 var t2 = t1; 

Using this line, you create a new string link as t2 , and this points to the same object with t1 , which is equal to "One"

 t2 = "Three"; 

Using this line, you create a string object named "Three" , and your t2 refers to this object. It no longer points to the "One" object. But this does not affect the t1 link. It still points to the "One" object.

That's why

 Console.WriteLine(t2); Console.WriteLine(t1); 

prints

 Three One 
+7
source share

Strings are also reference types. Therefore, if you write:

 var t1 = data[0]; 

You specified a new variable t1 , which refers to the same line as data[0] . After that you write:

 var t2 = t1; 

Now you have a new variable t2 , which refers to the same line as t1 . Now you have one String object on the heap and three references to this object: data[0] , t1 and t2 .

Then you write:

 t2 = "Three"; 

After this statement, t2 points to the antoher line in the heap with the value "Three" . However, data[0] and t1 still point to the same source string.

+7
source share

Writing t2 = "Three" will not change the value of t1, because the types are referential, so they "point" to their data. By assigning t2 , you tell him to refer to something else.

I love when you wrote t2 = t1 , you just say t2 to refer to the same as t1 . Future t2 destinations will simply refer to something else.

Some languages, such as C ++, have the ability to store a link to other variables, so when you change the link, you actually change the other variable.

0
source share

This program works exactly as you expect.

 void Main() { abc[] data = new[] { new abc(){i=1}, new abc(){i=2} }; var t1 = data[0]; var t2 = t1; // here is the difference // t2 is still pointing to its old location // but i will point a new position. t2.i = 5 ; //assigning the new value to i // but t2 still pointing to t1 //all will be identical now. Console.WriteLine(t2); Console.WriteLine(t1); Console.WriteLine(data[0]); // repeating like you. t2 = new abc() {i=444}; //now you will see t2 is different form t1. because // now t2 pointing to a new object instead of t1. Console.WriteLine(t2); Console.WriteLine(t1); } public class abc{ public int i ; } 
0
source share

when you assign one reference array variable to another, you are simply turning both variables into the same array. You are not invoking the creation of a copy of the array, and you are not invoking the contents of one array that needs to be copied to another.

// Assignment of array reference variables.

  using System; class AssignARef { static void Main() { int i; int[] nums1 = new int[10]; int[] nums2 = new int[10]; for(i=0; i < 10; i++) nums1[i] = i; for(i=0; i < 10; i++) nums2[i] = -i; Console.Write("Here is nums1: "); for(i=0; i < 10; i++) Console.Write(nums1[i] + " "); Console.WriteLine(); Console.Write("Here is nums2: "); for(i=0; i < 10; i++) Console.Write(nums2[i] + " "); Console.WriteLine(); nums2 = nums1; // now nums2 refers to nums1 Console.Write("Here is nums2 after assignment: "); for(i=0; i < 10; i++) Console.Write(nums2[i] + " "); Console.WriteLine(); // Next, operate on nums1 array through nums2. nums2[3] = 99; Console.Write("Here is nums1 after change through nums2: "); for(i=0; i < 10; i++) Console.Write(nums1[i] + " "); Console.WriteLine(); } } 

o / r

 Here is nums1: 0 1 2 3 4 5 6 7 8 9 Here is nums2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 Here is nums2 after assignment: 0 1 2 3 4 5 6 7 8 9 Here is nums1 after change through nums2: 0 1 2 99 4 5 6 7 8 9 
-one
source share

All Articles