다음을 통해 공유


StringBuilder.Capacity 속성

현재 인스턴스에 의해 할당된 메모리에 포함할 수 있는 최대 문자 수를 가져오거나 설정합니다.

네임스페이스: System.Text
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Property Capacity As Integer
‘사용 방법
Dim instance As StringBuilder
Dim value As Integer

value = instance.Capacity

instance.Capacity = value
public int Capacity { get; set; }
public:
property int Capacity {
    int get ();
    void set (int value);
}
/** @property */
public int get_Capacity ()

/** @property */
public void set_Capacity (int value)
public function get Capacity () : int

public function set Capacity (value : int)

속성 값

현재 인스턴스에 의해 할당된 메모리에 포함할 수 있는 최대 문자 수입니다.

예외

예외 형식 조건

ArgumentOutOfRangeException

Set 작업에 지정된 값이 이 인스턴스의 현재 길이보다 작은 경우

- 또는 -

Set 작업에 지정된 값이 최대 용량보다 큰 경우

설명

Capacity는 현재 인스턴스의 문자열 값에 영향을 주지 않습니다. CapacityLength보다 작지 않는 크기까지 줄일 수 있습니다.

StringBuilder는 공간이 필요할 경우 공간을 동적으로 더 할당하고 이에 따라 Capacity를 늘립니다. 성능을 높이기 위해 StringBuilder는 필요한 것보다 더 많은 메모리를 할당할 수 있습니다. 할당되는 메모리 양은 구현에 따라 달라집니다.

예제

다음 예제에서는 Capacity 속성을 보여 줍니다.

Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb1 As New StringBuilder("abc")
      Dim sb2 As 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))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'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 = 51
'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 = 51
'c2) sb2.Length = 3, sb2.Capacity = 51
'c3) sb1.ToString() = "", sb2.ToString() = "abc"
'c4) sb1 equals sb2: False
'
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));
    }
}
/*
This example produces the following results:

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 = 51
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 = 51
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
using namespace System;
using namespace System::Text;
int main()
{
   StringBuilder^ sb1 = gcnew StringBuilder( "abc" );
   StringBuilder^ sb2 = gcnew 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 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   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 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   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 = \"{0}\", sb2 = \"{1}\"", sb1, sb2 );
   Console::WriteLine( "c4) sb1 equals sb2: {0}", sb1->Equals( sb2 ) );
}

/*
This example produces the following results:

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 = 51
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 = 51
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/
// This example demonstrates StringBuilder.EnsureCapacity
//                           StringBuilder.Capacity
//                           StringBuilder.Length
//                           StringBuilder.Equals
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        StringBuilder sb1 = new StringBuilder("abc");
        StringBuilder sb2 = new StringBuilder("abc", 16);

        Console.WriteLine();
        Console.WriteLine("a1) sb1.get_Length() = {0}, " 
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("a2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("a3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("a4) sb1 equals sb2: {0}", 
            System.Convert.ToString(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.get_Length() = {0}, " 
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("b2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("b3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("b4) sb1 equals sb2: {0}", 
            System.Convert.ToString(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.set_Length(0);
        sb2.set_Capacity(51);

        Console.WriteLine();
        Console.WriteLine("c1) sb1.get_Length() = {0}, "
            + "sb1.get_Capacity() = {1}", 
            System.Convert.ToString(sb1.get_Length()), 
            System.Convert.ToString(sb1.get_Capacity()));
        Console.WriteLine("c2) sb2.get_Length() = {0}, " 
            + "sb2.get_Capacity() = {1}", 
            System.Convert.ToString(sb2.get_Length()), 
            System.Convert.ToString(sb2.get_Capacity()));
        Console.WriteLine("c3) sb1.ToString() = \"{0}\", " 
            + "sb2.ToString() = \"{1}\"", sb1.ToString(), sb2.ToString());
        Console.WriteLine("c4) sb1 equals sb2: {0}", 
            System.Convert.ToString(sb1.Equals(sb2)));
    } //main
} //Sample
/*
This example produces the following results:

a1) sb1.get_Length() = 3, sb1.get_Capacity() = 16
a2) sb2.get_Length() = 3, sb2.get_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.get_Length() = 3, sb1.get_Capacity() = 50
b2) sb2.get_Length() = 3, sb2.get_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.get_Length() = 0, sb1.get_Capacity() = 50
c2) sb2.get_Length() = 3, sb2.get_Capacity() = 51
c3) sb1.ToString() = "", sb2.ToString() = "abc"
c4) sb1 equals sb2: False
*/

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

StringBuilder 클래스
StringBuilder 멤버
System.Text 네임스페이스