C# String

不可变

1.IL代码优化字符串+操作

例1:

1)string s = "hello" + "world"; //IL代码会自动优化为一条指令 ldstr "helloworld" 加载到evaluation stack

2)string s1=“hello”; string s2="world"; string s=s1+s2; //不会优化 会分别加载s1 、 s2 但是IL代码是利用String.Contact()拼接  Contact方法内部先调用FastAllocateString开辟空间 然后执行Buffer.Memcpy

例2:

原文地址:https://www.cnblogs.com/fmys/p/10236552.html