In the past, if we used stringbuilder to reset it, we often used the new stringbuilder () method. In. Net 4.0, stringbuilder adds a clear () method. We can use:
1 StringBuilder stringBuilder = new StringBuilder();
2 stringBuilder.Append("Mock");
3 stringBuilder.Append("OK");
4 Console.WriteLine(stringBuilder.Length);
5 stringBuilder.Clear();
6 Console.WriteLine(stringBuilder.Length);
7 stringBuilder.Append("Mock");
8 stringBuilder.Append("OK");
What did this method do? Download reflector V6, which supports. Net 4.0. Check the implementation of this method. It turns out to be so simple:
public StringBuilder Clear()
{
this.Length = 0;
return this;
}
That is to say, in the. NET 3.5 environment, we can also copy it:
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("Time");
sb.Append("Home");
Console.WriteLine(sb.Length);
Console.WriteLine(sb.ToString());
sb.Length = 0;
sb.Append("Google Reader");
Console.WriteLine(sb.Length);
Console.WriteLine(sb.ToString());
}
Yes, it's so simple today, it's sent to the newbie zone,
See you later