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:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
Stack< string > myStack = new Stack< string >();
myStack.Push( "Geeks" );
myStack.Push( "Geeks Classes" );
myStack.Push( "Noida" );
myStack.Push( "Data Structures" );
myStack.Push( "GeeksforGeeks" );
Console.WriteLine(myStack.Contains( "GeeksforGeeks" ));
}
}
|
Example 2:
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
Stack< int > myStack = new Stack< int >();
myStack.Push(5);
myStack.Push(10);
myStack.Push(15);
myStack.Push(20);
myStack.Push(25);
Console.WriteLine(myStack.Contains(7));
}
}
|
Reference: