StringBuilder.MaxCapacity属性用于获取此实例的最大容量。
Syntax: public int MaxCapacity { get; }
Property Value: It returns the maximum number of characters of type System.Int32 this instance can hold.
注意:此实现的最大容量为Int32.MaxValue 。但是,此值是特定于实现的,并且在其他或更高版本的实现中可能会有所不同。您可以通过调用StringBuilder(Int32,Int32)构造函数来显式设置StringBuilder对象的最大容量。
例子:
// C# program to demonstrate
// the MaxCapacity Property
using System;
using System.Text;
class GFG {
// Main Method
public static void Main(String[] args)
{
// Create a StringBuilder object
// with a String passed as parameter
StringBuilder str1 =
new StringBuilder("GeeksforGeeks");
// printing the MaxCapacity of str1
Console.WriteLine("Maximum Capacity of str1 is: "
+ str1.MaxCapacity);
// Create a StringBuilder object
StringBuilder str2 = new StringBuilder();
// printing the MaxCapacity of str2
Console.WriteLine("Maximum Capacity of str2 is: "
+ str2.MaxCapacity);
}
}
输出:
Maximum Capacity of str1 is: 2147483647
Maximum Capacity of str2 is: 2147483647
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.text.stringbuilder.maxcapacity?view=netframework-4.7.2