如何在C#中查找StringBuilder的容量(示例)

StringBuilder.Capacity属性用于获取或设置当前实例分配的内存中可以包含的最大字符数。

语法:public int Capacity {get; set; }
返回值:该属性将返回当前实例分配的内存中可以包含的最大字符数。其值的范围可以从” 长度” 到” 最大容量” 。
异常:如果为set操作指定的值小于此实例的当前长度, 或者为set操作指定的值大于最大容量, 则此属性将提供ArgumentOutOfRangeException。
【如何在C#中查找StringBuilder的容量(示例)】下面的程序将说明上述属性的用法:
示例1:
//C# program to demonstrate //the Capacity() Property using System; using System.Text; class GFG {//Main Method public static void Main(String[] args) {//create a StringBuilder object, //default capacity will be 16 StringBuilder str = new StringBuilder(); //get default capacity int cap = str.Capacity; Console.WriteLine( "Deafult Capacity of StringBuilder = " + cap); //add the String to StringBuilder Object str.Append( "Geek" ); //get capacity cap = str.Capacity; //print the result Console.WriteLine( "StringBuilder = " + str); Console.WriteLine( "Current Capacity of StringBuilder = " + cap); } }

输出如下:
Deafult Capacity of StringBuilder = 16 StringBuilder = Geek Current Capacity of StringBuilder = 16

示例2:
//C# program to demonstrate //the Capacity() Property using System; using System.Text; class GFG { public static void Main(String[] args) {//create a StringBuilder object //with a String passed as parameter StringBuilder str = new StringBuilder( "WelcomeGeeks" ); //get capacity int capacity = str.Capacity; //print the result Console.WriteLine( "StringBuilder = " + str); Console.WriteLine( "Capacity of StringBuilder = " + capacity); } }

输出如下:
StringBuilder = WelcomeGeeks Capacity of StringBuilder = 16

参考:
  • https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.capacity?view=netframework-4.7.2

    推荐阅读