如何在 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("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2));
      Console.WriteLine("StringBuider1 capacity = "+strBuilder1.Capacity);
      Console.WriteLine("StringBuider1 maximum capacity = "+strBuilder1.MaxCapacity);
      Console.WriteLine("StringBuider2 capacity = "+strBuilder2.Capacity);
      Console.WriteLine("StringBuider2 maximum capacity = "+strBuilder2.MaxCapacity);
      Console.WriteLine("StringBuider3 capacity = "+strBuilder3.Capacity);
      Console.WriteLine("StringBuider3 maximum capacity = "+strBuilder3.MaxCapacity);
      Console.WriteLine("StringBuider4 capacity = "+strBuilder4.Capacity);
      Console.WriteLine("StringBuider4 maximum capacity = "+strBuilder4.MaxCapacity);
   }
}

输出

将生成以下输出 −

Is StringBuilder3 equal to StringBuilder2? = True
StringBuider1 capacity = 16
StringBuider1 maximum capacity = 2147483647
StringBuider2 capacity = 16
StringBuider2 maximum capacity = 2147483647
StringBuider3 capacity = 16
StringBuider3 maximum capacity = 2147483647
StringBuider4 capacity = 5
StringBuider4 maximum capacity = 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

更新于: 10-12-2019

174 次浏览

开启你的职业生涯

完成课程认证

立即开始
广告