在C#中创建具有指定容量的StringBuilder
本文向大家介绍在C#中创建具有指定容量的StringBuilder,包括了在C#中创建具有指定容量的StringBuilder的使用技巧和注意事项,需要的朋友参考一下
要创建具有指定容量的StringBuilder,代码如下-
示例
using System; using System.Text; public class Demo { public static void Main(String[] args){ StringBuilder strBuilder1 = new StringBuilder("Tim"); StringBuilder strBuilder2 = new StringBuilder("Tom"); StringBuilder strBuilder3 = new StringBuilder(); StringBuilder strBuilder4 = new StringBuilder(5); strBuilder2 = strBuilder3; Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2)); Console.WriteLine("StringBuider1 capacity = "+strBuilder1.Capacity); Console.WriteLine("StringBuider2 capacity = "+strBuilder2.Capacity); Console.WriteLine("StringBuider3 capacity = "+strBuilder3.Capacity); Console.WriteLine("StringBuider4 capacity = "+strBuilder4.Capacity); } }
输出结果
这将产生以下输出-
Is StringBuilder3 equal to StringBuilder2? = True StringBuider1 capacity = 16 StringBuider2 capacity = 16 StringBuider3 capacity = 16 StringBuider4 capacity = 5
示例
现在让我们来看另一个示例-
using System; using System.Text; public class Demo { public static void Main(String[] args){ StringBuilder strBuilder = new StringBuilder(); Console.WriteLine("StringBuider capacity = "+strBuilder.Capacity); StringBuilder strBuilder2 = new StringBuilder(20); Console.WriteLine("StringBuider2 capacity = "+strBuilder2.Capacity); } }
输出结果
这将产生以下输出-
StringBuider capacity = 16 StringBuider2 capacity = 20