以前我们使用StringBuilder如果要把它重置,往往是使用new StringBuilder()的方法;.NET 4.0 中StringBuilder添加一个Clear()方法;我们可以使用:
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 " );
这个方法到底做了什么呢?下载支持.Net 4.0的Reflector v6,查看这个方法的实现,原来这么简单:
public StringBuilder Clear()
{
this .Length = 0 ;
return this ;
}
也就是说在.net 3.5的环境中,我们自然也可以照搬过来:
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());
}
是的,今天就是这么简单,发到新手区去了,
回头再说,明天见
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=ligaoren"></script>
Tag标签:
stringbuilder 4.0 net