7
Reply

Reference type

Ask a question
Maha

Maha

11y
919
1
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)