Open In App

Stack.Equals() Method in C#

Last Updated : 28 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: csharp
// 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));
    }
}
Output:
True
Example 2: csharp
// 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));
    }
}
Output:
False
True

Next Article

Similar Reads