Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array in C# are reference type, why they acts as a value types?

According to MSDN, if all arrays are reference type, then why in the given sample code, the new value of t2 doesn't 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 treat several items as a single collection. The Microsoft® .NET Common Language Runtime (CLR) supports single-dimensional arrays, multidimensional arrays, and jagged arrays (arrays of arrays). All array types are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.

like image 624
nunu Avatar asked Mar 13 '15 12:03

nunu


3 Answers

One picture is worth a thousand words, so here is what's going on:

Before and After

The effect of the assignment of "Three" to t2 is that before the assignment t1 and t2 referenced the same object, but after the assignment they reference different objects. Nothing else is going on here.

The situation would have been different if you had an array of mutable objects, and manipulated their value instead of setting their references. For example, imagine replacing the array of strings with an array of StringBuilder objects, and calling t2.Replace("Two", "Three") instead of the assignment. Now the effect would be different, because t1, t2, and data[0] would be pointing to the same object.

like image 105
Sergey Kalinichenko Avatar answered Oct 16 '22 23:10

Sergey Kalinichenko


Your question has nothing to do with arrays. Since your data[0] is string -and it is a reference type- it's value is One, you never change it's value. You just created an another string reference called t2 and set point to same object with t1 reference. After you changed this reference object to "Three", but this won't effect to what t1 refers.

Let's look at line by line your code;

var t1 = data[0];

With this line you created a string reference as t1 and this points to "One" object.

var t2 = t1;

With this line, you create a new string reference as t2 and this points to same object with t1 which is "One"

t2 = "Three";

With this line, you create a string object called "Three" and your t2 is reference this object. It doesn't point to "One" object anymore. But this doesn't effect to t1 reference. It still points to "One" object.

That's why

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

prints

Three
One
like image 45
Soner Gönül Avatar answered Oct 16 '22 23:10

Soner Gönül


Strings are also reference types. So if you write:

var t1 = data[0];

You have declared a new variable t1 that references to the same string as data[0]. After this you write:

var t2 = t1;

Now you have a new variable t2 that references to the same string as t1. You now 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 antoher string in the heap with the value "Three". However, data[0] and t1 still point to the same original string.

like image 7
Elian Ebbing Avatar answered Oct 17 '22 01:10

Elian Ebbing