C# StringBuilder EnsureCapacity

Description

StringBuilder EnsureCapacity Ensures that the capacity of this instance of StringBuilder is at least the specified value.

Syntax

StringBuilder.EnsureCapacity has the following syntax.


public int EnsureCapacity(
  int capacity
)

Parameters

StringBuilder.EnsureCapacity has the following parameters.

  • capacity - The minimum capacity to ensure.

Returns

StringBuilder.EnsureCapacity method returns The new capacity of this instance.

Example

The following example demonstrates the EnsureCapacity method.


using System;//www  . j  av a 2 s.  c o  m
using System.Text;

class Sample 
{
    public static void Main() 
    {
        StringBuilder sb1 = new StringBuilder("abc");
        StringBuilder sb2 = new StringBuilder("abc", 16);
    
        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));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding