Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# Stack.Count 속성 완벽 정리 – 스택 요소 개수 확인 방법

C#에서 Stack.Count 속성은 스택(Stack)에 현재 포함되어 있는 요소의 총 개수를 가져오는 데 사용됩니다. 스택에 데이터가 몇 개 저장되어 있는지 확인해야 할 때 유용하게 활용할 수 있습니다.

구문(Syntax)

Stack.Count 속성의 기본 구문은 다음과 같습니다.

public virtual int Count { get; }

Count 속성은 읽기 전용(get 전용)이며, 반환 타입은 int입니다. 따라서 별도의 매개변수 없이 속성에 접근하기만 하면 스택 내 요소 개수를 즉시 얻을 수 있습니다.

예제 1: 문자열 스택에서 Count 사용하기

다음 예제는 문자열 요소를 담는 스택에서 Count 속성을 활용하는 방법을 보여줍니다.

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("Inspiron");
      stack.Push("Alienware");
      stack.Push("Projectors");
      stack.Push("Monitors");
      stack.Push("XPS");
      stack.Push("Laptop");
      stack.Push("Notebook");
      Console.WriteLine("Stack elements...");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = "+stack.Count);
      stack.Push("Ultrabook");
      stack.Push("Cameras");
      stack.Push("Keyboards");
      Console.WriteLine("\nStack elements...updated");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("\nCount of elements (updated) = "+stack.Count);
      stack.Clear();
      Console.Write("Count of elements (updated) = "+stack.Count);
   }
}

실행 결과

위 코드를 실행하면 다음과 같은 출력 결과를 얻을 수 있습니다.

Stack elements...
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements = 7
Stack elements...updated
Keyboards
Cameras
Ultrabook
Notebook
Laptop
XPS
Monitors
Projectors
Alienware
Inspiron
Count of elements (updated) = 10
Count of elements (updated) = 0

출력 결과를 보면 처음에는 7개의 요소가 있었고, Push 메서드로 3개의 요소를 추가한 후 Count 값이 10으로 변경된 것을 확인할 수 있습니다. 마지막으로 Clear() 메서드로 모든 요소를 제거하면 Count 값이 0이 됩니다.

예제 2: 정수 스택에서 Count 사용하기

이번에는 정수(int) 요소를 담는 스택으로 동일하게 Count 속성을 테스트해 보겠습니다.

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push(150);
      stack.Push(300);
      stack.Push(500);
      stack.Push(750);
      stack.Push(1000);
      stack.Push(1250);
      stack.Push(1500);
      stack.Push(2000);
      stack.Push(2500);
      Console.WriteLine("Stack elements...");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = "+stack.Count);
      stack.Push(3000);
      stack.Push(3500);
      stack.Push(4000);
      Console.WriteLine("\nStack elements...updated");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("\nCount of elements (updated) = "+stack.Count);
      stack.Clear();
      Console.Write("Count of elements (updated) = "+stack.Count);
   }
}

실행 결과

위 코드의 실행 결과는 다음과 같습니다.

Stack elements...
2500
2000
1500
1250
1000
750
500
300
150
Count of elements = 9
Stack elements...updated
4000
3500
3000
2500
2000
1500
1250
1000
750
500
300
150
Count of elements (updated) = 12
Count of elements (updated) = 0

정리

Stack.Count 속성의 핵심 특징을 정리하면 다음과 같습니다.

  • 스택에 저장된 요소의 개수를 int 타입으로 반환합니다.
  • 읽기 전용 속성이므로 직접 값을 설정할 수 없습니다.
  • Push로 요소를 추가하면 Count 값이 증가하고, Pop 또는 Clear로 요소를 제거하면 감소합니다.
  • 스택이 비어 있으면 Count는 0을 반환합니다.

이처럼 Stack.Count 속성은 스택의 상태를 파악하고, 반복문 처리나 빈 스택 여부 검사 등 다양한 상황에서 활용할 수 있는 기본적이면서도 중요한 기능입니다.