本文整理汇总了C#中System.Text.StringBuilder.Capacity属性的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.Capacity属性的具体用法?C# StringBuilder.Capacity怎么用?C# StringBuilder.Capacity使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Text.StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.Capacity属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Text;
class Sample
{
public static void Main()
{
StringBuilder sb1 = new StringBuilder("abc");
StringBuilder sb2 = new StringBuilder("abc", 16);
Console.WriteLine();
Console.WriteLine("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("a3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("a4) sb1 equals sb2: {0}", sb1.Equals(sb2));
Console.WriteLine();
Console.WriteLine("Ensure sb1 has a capacity of at least 50 characters.");
sb1.EnsureCapacity(50);
Console.WriteLine();
Console.WriteLine("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("b3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("b4) sb1 equals sb2: {0}", sb1.Equals(sb2));
Console.WriteLine();
Console.WriteLine("Set the length of sb1 to zero.");
Console.WriteLine("Set the capacity of sb2 to 51 characters.");
sb1.Length = 0;
sb2.Capacity = 51;
Console.WriteLine();
Console.WriteLine("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity);
Console.WriteLine("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity);
Console.WriteLine("c3) sb1.ToString() = \"{0}\", sb2.ToString() = \"{1}\"",
sb1.ToString(), sb2.ToString());
Console.WriteLine("c4) sb1 equals sb2: {0}", sb1.Equals(sb2));
}
}
输出:
a1) sb1.Length = 3, sb1.Capacity = 16 a2) sb2.Length = 3, sb2.Capacity = 16 a3) sb1.ToString() = "abc", sb2.ToString() = "abc" a4) sb1 equals sb2: True Ensure sb1 has a capacity of at least 50 characters. b1) sb1.Length = 3, sb1.Capacity = 50 b2) sb2.Length = 3, sb2.Capacity = 16 b3) sb1.ToString() = "abc", sb2.ToString() = "abc" b4) sb1 equals sb2: False Set the length of sb1 to zero. Set the capacity of sb2 to 51 characters. c1) sb1.Length = 0, sb1.Capacity = 50 c2) sb2.Length = 3, sb2.Capacity = 51 c3) sb1.ToString() = "", sb2.ToString() = "abc" c4) sb1 equals sb2: False
示例2: Main
//引入命名空间
using System;
using System.Text;
class StringApp {
static void Main(string[] args) {
StringBuilder myBuffer = new StringBuilder("My string data");
Console.WriteLine(myBuffer.Capacity);
myBuffer.Append(" contains some numerical data: ");
myBuffer.AppendFormat("{0}, {1}.", 44, 99);
Console.WriteLine(myBuffer.Capacity);
Console.WriteLine(myBuffer);
}
}