1. Use String. Empty to assign an initial value to a null string variable
2, use Str. Length = = 0 Short string comparison
- The quickest method: if (str. Length = = 0)
- Second: if (str = = String.Empty) or if (str = = "")
3, avoid unnecessary string toupper, ToLower class operation
4, skillfully use the StringBuilder string splicing operation
5. Create StringBuilder should specify initial size
6. Avoid misuse of StringBuilder
- string concatenation operations such as STR1+STR2+STR3+STR4 are compiled into String.Concat (STR1,STR2,STR3, STR4), but are more efficient than StringBuilder. String.Concat will determine the length of the string once, StringBuilder need to do resize, suitable for multiple generation of the string object case.
7, through the direct setting. Length=0 to initialize StringBuilder
8, do not use. Length=0 to release StringBuilder-occupied memory
static void Test () { StringBuilder sb = new StringBuilder (n); for (int i = 0; i <; i++) { sb. Append (i); } String t = sb. ToString (); ...//other code snippets that do not use the variable SB . Length = 0; Remove the sentence manually emptying SB code, will release memory earlier}
9. To be Continued
Copyright: jiankunking Source: http://blog.csdn.net/jiankunking This article is copyright to the author and Csdn, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original connection.
C # string manipulation--Reduce garbage collection pressure