C# | Get the number of elements contained in the Stack Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Stack represents a last-in, first out collection of object. Stack<T>.Count Property is used to gets the number of elements contained in the Stack. Retrieving the value of this property is an O(1) operation. Syntax: myStack.Count Here myStack is the name of the Stack<T> Return Value: The property returns the number of elements contained in the Stack<T>. Example 1: CSHARP // C# code to Get the number of // elements contained in the Stack using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a Stack of strings Stack<string> myStack = new Stack<string>(); // Inserting the elements into the Stack myStack.Push("Chandigarh"); myStack.Push("Delhi"); myStack.Push("Noida"); myStack.Push("Himachal"); myStack.Push("Punjab"); myStack.Push("Jammu"); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements in the Stack are : "); Console.WriteLine(myStack.Count); } } Output: Total number of elements in the Stack are : 6 Example 2: CSHARP // C# code to Get the number of // elements contained in the Stack using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a Stack of Integers Stack<int> myStack = new Stack<int>(); // Displaying the count of elements // contained in the Stack Console.Write("Total number of elements in the Stack are : "); // The function should return 0 // as the Stack is empty and it // doesn't contain any element Console.WriteLine(myStack.Count); } } Output: Total number of elements in the Stack are : 0 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.count?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Get the number of elements contained in the Stack S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-Generic-Stack CSharp-Collections-Namespace CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Check if a Stack contains an element Stack represents a last-in, first out collection of object. Stack<T>.Contains(Object) Method is used to check whether an element is in the Stack<T> or not. Syntax: public virtual bool Contains(object obj); Return Value: The function returns True if the element exists in the Stack<T 2 min read C# | Insert an object at the top of the Stack - Push Operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Push(T) Method is used to inserts an object 2 min read C# | Get object at the top of the Stack - Peek operation Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Peek Method is used to returns the object a 3 min read C# | Stack<T>.TrimExcess Method with Examples Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.TrimExcess Method is used to set the capacity 2 min read C# Program to Get Current Stack Trace Information Using Environment Class In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information, 2 min read Like