Preface
Generally, programmers have the most
"Strong"The two weapons are copy and paste.
StringThis article is intended to introduce some tips. We know, or we knew the string type when we learned C # On the first day, and soon we knew the simplest way to assemble strings:
string s = string.Empty;s += "Bruce";s += "Who?";s += "a guy think sth different...";
This writing method is too simple and beautiful, because it is too happy to use C # compared to strcat in C language. I don't know when this statement is supported:
s += "Bruce" + " Lee";
(I really don't know when to start to support the above statements. Remember that. Net2.0 cannot be written in this way. If it is. Net1.1, it must be enclosed in brackets:
s += ("Bruce" + " Lee");
)
StringBuilderHowever, this happiness may cause beginners to be unaware of it.
StringBuilderIs occasionally seen in other people's articles.
StringBuilderMay also not understand why "+ =" is not required. You need to write a bunch of Append...
StringBuilder sb = new StringBuilder();sb.Append("Bruce");sb.Append("Who?");sb.Append("a guy think sth different...");
Maybe you don't believe it. I guess some of my friends who have been working for. Net for a year or two in the enterprise still don't know.
StringBuilder. Especially in Asp. Net, many friends like to use C # to assemble a bunch of front-end code, such as Html statements, JavaScript, and even SQL statements... Without exception, they all use "+ =" to splice, and then pat the ass and leave, leaving dozens or even hundreds of lines of such statements. We should know that using the first method to concatenate strings is far inferior in terms of time or space performance.
StringBuilder. When the child sees this, he may quickly go back and replace his section "+ =" with the StringBuilder Append method. Don't worry. After all, it is time-consuming to switch from mode to Mode 2. It is definitely hard work. Now I want to tell you how to quickly increase the performance of method 1 code to Mode 2.
StringJoinerIt's easy to change string s
StringJoinerS is a success. That is
StringJoiner s = string.Empty;s += "Bruce";s += "Who?";s += "a guy think sth different...";
Why is it so simple to improve high performance? Fooling me? Of course it's not just a matter of thumb or magic. There will be performance testing data at the end of the article, and there will be a picture of the truth. Let me unveil it now
StringJoinerIn the end, you will certainly say: it was so simple.
StringJoinerIn fact, it is similar to the decorator mode, which hides StringBuilder, just as the magician prefers to hide a bunch of items to the sleeves to hide the audience.
public class StringJoiner { protected StringBuilder Builder; public StringJoiner() { Builder = new StringBuilder(); }}
(Define Builder as protected and expect to inherit from it someday
StringJoiner) How does StringJoiner implement the Code just now?
1. implicit conversionFor example:
StringJoiner s = string.Empty;
The answer is:
public static implicit operator StringJoiner(string value){ StringJoiner text = new StringJoiner(); text.Builder.Append(value); return text;}
(Create an object and transfer the value assignment to the Append method of StringBuilder)
2. Overload OperatorsFor example:
s += "Bruce";
The answer is:
public static StringJoiner operator +(StringJoiner self, string value){ self.Builder.Append(value); return self;}
(Implementation
StringJoinerAnd
StringYou can use operators to specify the type.
"+"To be more common, such as connecting to other types:
s += 123;s += 0.314;s += c;
Therefore, you must reload
StringJoiner+
Object
public static StringJoiner operator +(StringJoiner self, object value){ self.Builder.Append(value); return self;}
Finally, in order
StringJoinerThe "Flickering" level is upgraded to the next level,
StringJoinerObjects must be implicitly converted to the string type:
public static implicit operator string(StringJoiner value){ return value.ToString();}public override string ToString(){ return this.Builder.ToString();}
Of course, at this moment
StringJoinerIt is enough. A more thorough "flicker" is not the purpose of this Article.
StringJoinerInstead of replacing string, or StringBuilder, I agree to use StringBuilder. However, if you want to refactor a piece of code that is "string + =", use
StringJoinerRight.
Performance TestingPerformance Counter: CodeTimer (XP version) test code (assemble 10 thousand strings in three different methods, then assemble 10 thousand integers, and repeat more than 10 operations ):
Static void Main (string [] args) {CodeTimer. time ("", 1, () =>{}); // use + = to assemble the CodeTimer string. time ("NormalString", 10, () => NormalString (); // StringJoiner assembled string CodeTimer. time (