- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
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