In the code below, changes on cpy
value does not affect ori
value, so string does not behave like a reference type :
string ori = "text 1";
string cpy = ori;
cpy = "text 2";
Console.WriteLine("{0}", ori);
but, a class has a different behaviour :
class WebPage
{
public string Text;
}
// Now look at reference type behaviour
WebPage originalWebPage = new WebPage();
originalWebPage.Text = "Original web text";
// Copy just the URL
WebPage copyOfWebPage = originalWebPage;
// Change the page via the new copy of the URL
copyOfWebPage.Text = "Changed web text";
// Write out the contents of the page
// Output=Changed web text
Console.WriteLine ("originalWebPage={0}",
originalWebPage.Text);
can someone tell me why the behaviour is different between a class and string while the two of them are reference types ?