Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 StringBuilder의 용량을 찾는 방법

<시간/>

C#에서 StringBuilder의 용량을 찾으려면 코드는 다음과 같습니다. -

예시

using System;
using System.Text;
public class Demo {
   public static void Main(String[] args) {
      StringBuilder strBuilder1 = new StringBuilder("Tim");
      StringBuilder strBuilder2 = new StringBuilder("Tom");
      StringBuilder strBuilder3 = new StringBuilder();
      Console.WriteLine("Capacity of StringBuilder1 = "+strBuilder1.Capacity);
      Console.WriteLine("Capacity of StringBuilder2 = "+strBuilder2.Capacity);
      Console.WriteLine("Capacity of StringBuilder3 = "+strBuilder3.Capacity);
      strBuilder2 = strBuilder3;
      Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2));
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Capacity of StringBuilder1 = 16
Capacity of StringBuilder2 = 16
Capacity of StringBuilder3 = 16
Is StringBuilder3 equal to StringBuilder2? = True

예시

다른 예를 보겠습니다 -

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("StringBuider2 capacity = "+strBuilder2.Capacity);
      Console.WriteLine("StringBuider3 capacity = "+strBuilder3.Capacity);
      Console.WriteLine("StringBuider4 capacity = "+strBuilder4.Capacity);
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

Is StringBuilder3 equal to StringBuilder2? = True
StringBuider1 capacity = 16
StringBuider2 capacity = 16
StringBuider3 capacity = 16
StringBuider4 capacity = 5