Stack.Count Property in C#
This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to Count. Retrieving the value of this property is an O(1) operation.
Syntax:
public virtual int Count { get; }
Return Value: It returns the number of elements contained in the Stack having type System.Int32.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# code illustrate the // Stack.Count Property using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // 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); } } |
Total number of elements in the Stack are : 6
Example 2:
// C# code illustrate the // Stack.Count Property using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // 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); } } |
Total number of elements in the Stack are : 0
Reference:
Recommended Posts:
- C# | SortedDictionary.Keys Property
- C# | SortedDictionary.Count Property
- C# | SortedDictionary.Values Property
- C# | Dictionary.Values Property
- C# | Dictionary.Keys Property
- C# | Dictionary.Count Property
- C# | Dictionary.Item[] Property
- C# | SortedDictionary.Item[] Property
- How to use AutoCheck property of CheckBox in C#?
- How to use AcceptsReturn Property of TextBox in C#?
- Console.KeyAvailable() Property in C#
- Queue.Count Property in C#
- Queue.IsSynchronized Property in C#
- Stack.IsSynchronized Property in C#
- C# | LongLength property of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



