Do string literals get optimised by the compiler?

后端 未结 3 1087
春和景丽
春和景丽 2020-11-30 11:34

Does the C# compiler or .NET CLR do any clever memory optimisation of string literals/constants? I could swear I\'d heard of the concept of \"string internalisation\" so th

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 12:07

    Yes it does optimize string literals. One simple example where you can see that:

    string s1="A";
    string s2="A";
    object.ReferenceEquals(s1,s2);  //true
    

提交回复
热议问题