If strings in .NET are reference types, in the below code, why doesn't string2 change to "hi" after string1 is changed?
static void IsStringReallyAReference()
{
string string1 = "hello";
string string2 = string1;
Console.WriteLine("-- Strings --");
Console.WriteLine(string1);
Console.WriteLine(string2);
string1 = "hi";
Console.WriteLine(string1);
Console.WriteLine(string2);
Console.Read();
}
/*Output:
hello
hello
hi
hello*/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…