String type

 

string s = "a" + "b";
string s1 = "b";
string s2 = "a" + s1;

 

The IL code for the above code is as follows:

 

It can be seen from the line of code IL_0001:ldstr "ab" that when the string assignment uses a literal value, the C# compiler has already performed the connection operation when compiling;

As can be seen from the IL code of s1 and s2, using the + operator for non-literal strings, the concatenation occurs at runtime, which causes multiple objects to be created on the heap at runtime, and the heap is required. Garbage collected, has an impact on performance. Therefore, the StringBuilder type should be used.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326912396&siteId=291194637