.NET 運行時維護一張字符串驻留表,存儲編譯期常量字符串及運行時調用 String.Intern 產生的字符串。相同內容的字符串在池中唯一實例。
String.Intern(str) 將字符串加入池,返回池中引用。
string a = "hello";
string b = "he" + "llo"; // 編譯期常量折疊,同一實例
Console.WriteLine(Object.ReferenceEquals(a, b)); // True
string c = Console.ReadLine(); // 運行時輸入
string d = String.Intern(c); // 手動入池
Console.WriteLine(Object.ReferenceEquals(a, d)); // 若輸入 hello 則 True
https://www.thinksaas.cn/group/topic/69377/
具體可以去看clr via C#