Data type string is a reference type. Therefore it will not create any copy in program, changes alter the result. In the following program o/p expected was:
X
Copy X
Copy A
But o/p was:
X
Copy X
Copy X
using System;
class Program
{
static void Main()
{
string s = "X";
string copy = s;
Console.WriteLine(s);
Console.WriteLine("Copy " + copy);
s = "A"; //Now "s" has been changed
Console.WriteLine("Copy " + copy);
Console.ReadKey();
}
}
/*
X
Copy X
Copy X
*/
Loading

VulpesRank #55 · 29.2K reputationPosted Sep 26, 2013, 7:31 AM EDT
Posted Sep 26, 2013, 7:51 AM EDT
Imtiyaz54 reputationPosted Sep 26, 2013, 8:24 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 26, 2013, 9:58 AM EDT
Posted Sep 26, 2013, 11:35 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 26, 2013, 12:38 PM EDT
Unfortunately, all the structs I can think of in the .NET framework are immutable - they can't be changed - and so too is the String class.
So, in my example, I thought I'd create a simple 'mutable' class and struct to illustrate the difference.
Posted Sep 26, 2013, 12:41 PM EDT