Stack.Equals() Method in C#

Equals(Object) Method which is inherited from the Object class is used to check if a specified Stack class object is equal to another Stack class object or not. This method comes under the System.Collections namespace.

Syntax:

public virtual bool Equals (object obj);

Here, obj is the object which is to be compared with the current object.



Return Value: This method return true if the specified object is equal to the current object otherwise it returns false.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if two Stack
// class objects are equal or not
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack named st1
        Stack st1 = new Stack();
  
        // Adding elements to st1
        st1.Push(1);
        st1.Push(2);
        st1.Push(3);
        st1.Push(4);
  
        // Checking whether st1 is
        // equal to itself or not
        Console.WriteLine(st1.Equals(st1));
    }
}

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if two Stack
// class objects are equal or not
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack named st1
        Stack st1 = new Stack();
  
        // Adding elements to the Stack
        st1.Push("C");
        st1.Push("C++");
        st1.Push("Java");
        st1.Push("C#");
  
        // Creating a Stack named st2
        Stack st2 = new Stack();
  
        st2.Push("HTML");
        st2.Push("CSS");
        st2.Push("PHP");
        st2.Push("SQL");
  
        // Checking whether st1 is
        // equal to st2 or not
        Console.WriteLine(st1.Equals(st2));
  
        // Creating a new Stack
        Stack st3 = new Stack();
  
        // Assigning st2 to st3
        st3 = st2;
  
        // Checking whether st3 is
        // equal to st2 or not
        Console.WriteLine(st3.Equals(st2));
    }
}

chevron_right


Output:

False
True


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.