C# Stack - Count Property



The C# stack Count() property is used to count the number of elements available in the stack.

If you think count is the capacity of the stack, then you are wrong. The count is not the capacity of the stack. The count is the number of elements that are actually in the Stack, while the capacity is the number of elements that the Stack can store.

The capacity is always greater than or equal to the count. If the count exceeds the capacity while adding elements, the capacity will automatically be increased.

Syntax

Following is the syntax of the C# stack Count() property −

public virtual int Count {get;}

Parameters

Property does not accepts any parameters.

Return value

This property returns the number of elements the stack contains.

Example 1: Count The Elements in Stack

Following is the basic example of the Count property, demonstrating how we can use it to obtain the number of elements −

    
using System;
using System.Collections;
public class Example {		
   public static void Main(string[] args) {
      Stack myStack = new Stack();
      
      // Insert elements into Stack
      myStack.Push(1);
      myStack.Push(2);
      myStack.Push(3);
      myStack.Push(4);
      
      // Print Stack elements
      Console.Write("My Stack: ");
      foreach (var item in myStack) {
         Console.Write(item + " ");
      }
      
      //Count the elements
      int count = myStack.Count;
      Console.Write("\nNumber of elements: "+ count);
   }
}

Output

Following is the output −

My Stack: 4 3 2 1 
Number of elements: 4

Example 2: Count The Number of Char in Stack

Let's see another example of the Count property to display the number of characters this stack contains −

using System;
using System.Collections.Generic;

public class Example {		
  public static void Main(string[] args) {
    Stack<char> myStack = new Stack<char>();
    
    // Insert elements into Stack
    myStack.Push('a');
    myStack.Push('b');
    myStack.Push('c');
    myStack.Push('d');
    
    // Print Stack elements
    Console.Write("My Stack: ");
    foreach (var item in myStack) {
        Console.Write(item + " ");
    }
    
    // Count the elements
    int count = myStack.Count;
    Console.Write("\nNumber of characters: " + count);
  }
}

Output

Following is the output −

My Stack: d c b a 
Number of characters: 4

Example 3: Counting Before and After Removal

Here, in this example, we will display the count before and after the removal of elements in the stack −

using System;
using System.Collections;

public class Example {		
   public static void Main(string[] args) {
      Stack myStack = new Stack();
      
      // Insert elements into Stack
      myStack.Push('a');
      myStack.Push('b');
      myStack.Push('c');
      myStack.Push('d');
      
      // Count the elements
      int count = myStack.Count;
      Console.Write("Count Before Poping: " + count);
      
      // Remove an element from top
      myStack.Pop();
      
      // Count After Pop
      Console.Write("\nCount After Poping: " + myStack.Count);
   }
}

Output

Following is the output −

Count Before Poping: 4
Count After Poping: 3
csharp_stack.htm
Advertisements