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
Posted Sep 26, 2013, 4:41 PM
VulpesPosted Sep 26, 2013, 4:38 PM
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, 3:35 PM
VulpesPosted Sep 26, 2013, 1:58 PM
ImtiyazPosted Sep 26, 2013, 12:24 PM
Posted Sep 26, 2013, 11:51 AM
VulpesPosted Sep 26, 2013, 11:31 AM