C# 所有類別的基底類別。object 參考可繫結任何子型別物件。提供 Equals()、GetHashCode()、GetType() 等通用方法,常需覆寫(Override)。
string str1 = "GoSoA.com.cn";
string str2 = str1;
Console.WriteLine("str1=" + str1); // GoSoA.com.cn
Console.WriteLine("str2=" + str2); // GoSoA.com.cn
str1 = "www.GoSoA.com.cn";
Console.WriteLine("str1=" + str1); // www.GoSoA.com.cn
Console.WriteLine("str2=" + str2); // GoSoA.com.cn(未改變)
為何不像一般參考型別?
初始化 str1 時在堆積配置字串物件,str2 指向同一物件。修改 str1 時並非改動原物件,而是新建物件,str2 仍指向原物件,故值不變。這是 string 不可變特性所致。