我想知道特定于 .Net 框架的字符串实习的过程和内部结构。还想知道使用实习的好处以及我们应该使用字符串实习来提高性能的场景/情况。虽然我从 Jeffery Richter 的 CLR 书中学习过实习,但我仍然很困惑,想更详细地了解它。
[编辑] 使用示例代码提出特定问题,如下所示:
private void MethodA()
{
string s = "String"; // line 1 - interned literal as explained in the answer
//s.intern(); // line 2 - what would happen in line 3 if we uncomment this line, will it make any difference?
}
private bool MethodB(string compareThis)
{
if (compareThis == "String") // line 3 - will this line use interning (with and without uncommenting line 2 above)?
{
return true;
}
return false;
}