C# Stack - Contains() Method



The C# stack Contains() method is used to determine whether an element is available in the stack.

The method performs the linear search, so it takes O(n)complexity, where n is the count.

Syntax

Following is the syntax of the C# stack Contains() method −

public virtual bool Contains (object? obj);

Parameters

This method accepts an obj, which represents the element located in the stack. The value can be null.

Return value

This method returns true if the element is found in the stack. Otherwise, false.

Example 1: Using the Contains() Method

Following is the basic example of the Contains() method to find an element in the stack −

    
using System;
using System.Collections;
class Example {
   public static void Main() {
      Stack myStack = new Stack();
      
      // Inserting the elements into the Stack
      myStack.Push(1);
      myStack.Push(2);
      myStack.Push(3);
      myStack.Push(4);
      myStack.Push(5);

      int element = 4;
      bool res = myStack.Contains(element);

      Console.WriteLine($"Is the {element} available?: {res}");
   }
}

Output

Following is the output −

Is the 4 available?: True

Example 2: Check Whether String is Available

Let's see another example of the Contains() method to check whether the string is available in the stack or not −

using System; 
using System.Collections; 

class Example { 
   public static void Main() { 
      Stack myStack = new Stack(); 

      myStack.Push("Hello"); 
      myStack.Push("tutorialspoint"); 
      myStack.Push("India"); 
      myStack.Push("Good to know");
      
      string element = "India";

      bool res = myStack.Contains(element); 
      
      Console.WriteLine($"Is the {element} available?: {res}");
   }
}

Output

Following is the output −

Is the India available?: True

Example 3: Check Person's Details in Stack

The below example creates a person object. Here, we use the Contains() method to verify if a person's details are available or not −

using System;
using System.Collections.Generic;
class Person {
   public string Name { get; set; }
   public int Age { get; set; }

   public Person(string name, int age) {
      Name = name;
      Age = age;
   }

   public override string ToString() {
      return $"Name: {Name}, Age: {Age}";
   }
   // Override Equals and GetHashCode to compare objects by value instead of reference
   public override bool Equals(object obj) {
      if (obj is Person other) {
         return Name == other.Name && Age == other.Age;
      }
      return false;
   }

   public override int GetHashCode() {
      return HashCode.Combine(Name, Age);
   }
}
class Example {
   static void Main() {
      // Create a stack of Person objects
      Stack<Person> people = new Stack<Person>();
   
      // Push Person objects onto the stack
      people.Push(new Person("Aman", 25));
      people.Push(new Person("Gupta", 24));
      people.Push(new Person("Vivek", 26));
   
      Console.WriteLine("People stack before Contains:");
      foreach (var person in people) {
         Console.WriteLine(person);
      }
      
      // use the contains method
      bool res = people.Contains(new Person("Aman", 25));
      Console.Write("Is this object avialable: " + res);
   }
}

Output

Following is the output −

People stack before Contains:
Name: Vivek, Age: 26
Name: Gupta, Age: 24
Name: Aman, Age: 25
Is this object avialable: True
csharp_stack.htm
Advertisements