Maha

Maha

  • NA
  • 0
  • 325k

Reference type

Sep 26 2013 11:22 AM
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
*/


Answers (7)