在C#中如何找到StringBuilder的MaxCapacity(最大容量)?
本文向大家介绍在C#中如何找到StringBuilder的MaxCapacity(最大容量)?,包括了在C#中如何找到StringBuilder的MaxCapacity(最大容量)?的使用技巧和注意事项,需要的朋友参考一下
要找到StringBuilder的MaxCapacity,代码如下-
示例
using System; using System.Text; public class Demo { public static void Main(String[] args) { StringBuilder strBuilder1 = new StringBuilder("Amy"); StringBuilder strBuilder2 = new StringBuilder("Katie"); StringBuilder strBuilder3 = new StringBuilder(); StringBuilder strBuilder4 = new StringBuilder(5); strBuilder2 = strBuilder3; Console.WriteLine("StringBuilder 3是否等于StringBuilder 2? = "+strBuilder3.Equals(strBuilder2)); Console.WriteLine("StringBuider1 容量 = "+strBuilder1.Capacity); Console.WriteLine("StringBuider1 最大容量 = "+strBuilder1.MaxCapacity); Console.WriteLine("StringBuider2 容量 = "+strBuilder2.Capacity); Console.WriteLine("StringBuider2 最大容量 = "+strBuilder2.MaxCapacity); Console.WriteLine("StringBuider3 容量 = "+strBuilder3.Capacity); Console.WriteLine("StringBuider3 最大容量 = "+strBuilder3.MaxCapacity); Console.WriteLine("StringBuider4 容量 = "+strBuilder4.Capacity); Console.WriteLine("StringBuider4 最大容量 = "+strBuilder4.MaxCapacity); } }
输出结果
这将产生以下输出-
StringBuilder 3是否等于StringBuilder 2? = True StringBuider1 容量 = 16 StringBuider1 最大容量 = 2147483647 StringBuider2 容量 = 16 StringBuider2 最大容量 = 2147483647 StringBuider3 容量 = 16 StringBuider3 最大容量 = 2147483647 StringBuider4 容量 = 5 StringBuider4 最大容量 = 2147483647
示例
让我们看另一个例子-
using System; using System.Text; public class Demo { public static void Main(String[] args) { StringBuilder strBuilder = new StringBuilder("Amy"); Console.WriteLine("StringBuider capacity = "+strBuilder.Capacity); Console.WriteLine("StringBuider length = "+strBuilder.Length); Console.WriteLine("StringBuider maximum capacity = "+strBuilder.MaxCapacity); } }
输出结果
这将产生以下输出-
StringBuider capacity = 16 StringBuider length = 3 StringBuider maximum capacity = 2147483647