C# | Check if a Stack contains an element 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>.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> and returns False if the element doesn't exist in the Stack. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to Check if a Stack // contains an element 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("Geeks"); myStack.Push("Geeks Classes"); myStack.Push("Noida"); myStack.Push("Data Structures"); myStack.Push("GeeksforGeeks"); // Checking whether the element is // present in the Stack or not // The function returns True if the // element is present in the Stack, else // returns False Console.WriteLine(myStack.Contains("GeeksforGeeks")); } } Output: True Example 2: CSHARP // C# code to Check if a Stack // contains an element 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>(); // Inserting the elements into the Stack myStack.Push(5); myStack.Push(10); myStack.Push(15); myStack.Push(20); myStack.Push(25); // Checking whether the element is // present in the Stack or not // The function returns True if the // element is present in the Stack, else // returns False Console.WriteLine(myStack.Contains(7)); } } Output: False Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if a Stack contains an element S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-Stack CSharp-Collections-Namespace CSharp-Generic-Namespace +2 More Practice Tags : Misc Similar Reads C# | Create a Stack from a collection 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. Creation of stack means the addition of item into the stac 3 min read Stack.Contains() Method in C# This method(comes under System.Collections namespace) is used to check whether a specified element is present is Stack or not. Internally this method checks for equality by calling the Object.Equals method. Also, it performs a linear search, therefore, this method is an O(n) operation, where n is Co 2 min read C# | Get the number of elements contained in the Stack 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 2 min read Implementing Stack in C# Stack is a linear data structure. It follows LIFO(Last In First Out) pattern for Input/output. Following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be a stack Overflow condition. Pop: Removes an item from the stack. Th 2 min read stack empty() and stack size() in C++ STL The std::stack::size() and std::stack::empty() in C++ are built-in functions that are used to provide information about the size of the stack. They are the member functions of the std::stack container defined inside <stack> header file.stack::empty()The stack::empty() method is used to check w 2 min read Like