Open In App

Stack.Equals() Method in C#

Last Updated : 28 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
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:




// 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:




// 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


Previous Article
Next Article

Similar Reads

C# | Equals(String, String) Method
In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo methods. This method compares two string on the basis of
2 min read
C# | Char.Equals() Method
In C#, Char.Equals() is a System.Char struct method which is used to return a value by checking whether current instance is equal to a specified object or Char value. This method can be overloaded by passing different type of arguments to it. Char.Equals(Char) Method Char.Equals(Object) Method Char.Equals(Char) Method This method is used to returns
3 min read
C# | Boolean.Equals(Boolean) Method
This method is used to return a value indicating whether this instance is equal to a specified Boolean object.Syntax: public bool Equals (bool obj); Here, obj is a boolean value to compare to this instance.Return Value: This method returns true if obj has the same value as this instance otherwise it returns false.Below programs illustrate the use o
2 min read
C# | Byte.Equals(Byte) Method
This method is used to return a value indicating whether this instance and a specified Byte object represent the same value. Syntax: public bool Equals (byte obj); Here, obj is a byte object to compare to this instance. Return Value: This method returns true if obj is equal to this instance otherwise, it returns false. Below programs illustrate the
2 min read
Queue.Equals() Method in C#
Equals(Object) Method which is inherited from the Object class is used to check if a specified Queue class object is equal to another Queue 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 Va
2 min read
DateTime.Equals() Method in C#
This method is used to get a value indicating whether two DateTime objects, or a DateTime instance and another object or DateTime, have the same value. There are total 3 methods in the overload list of this method: Equals(DateTime, DateTime)Equals(DateTime)Equals(Object)Equals(DateTime, DateTime) This method is used to return a value indicating whe
5 min read
Double.Equals() Method in C# with Examples
Double.Equals() Method is used to get a value that indicates whether the two instances of Double represent the same value or not. There are total of two methods in the overload list of this method as follows: Equals(Double) MethodEquals(Object) MethodDouble.Equals(Double) This method is used to return a value indicating whether this instance and a
3 min read
Int16.Equals Method in C# with Examples
Int16.Equals() Method is used to get a value which indicates whether the current instance is equal to a specified object or Int16. There are 2 methods in the overload list of this method which are as follows: Equals(Int16) Method Equals(Object) Method Int16.Equals(Int16) This method is used to return a value indicating whether the current instance
4 min read
C# | Byte.Equals(Object) Method
This method is used to get a value which indicates whether the current instance is equal to a specified object or not. Syntax: public override bool Equals (object obj); Here, it takes an object to compare with the current instance or null. Return Value: This method returns true if obj is an instance of Byte and equals the value of current instance
2 min read
Int32.Equals Method in C# with Examples
Int32.Equals() Method is used to get a value which indicates whether the current instance is equal to a specified object or Int32 or not. There are two methods in the overload list of this method as follows: Equals(Int32) Method Equals(Object) Method Int32.Equals(Int32) This method is used to return a value indicating whether the current instance i
3 min read