📜  C#中的Stack.Equals()方法

📅  最后修改于: 2021-05-30 00:18:54             🧑  作者: Mango

从Object类继承的Equals(Object)方法用于检查指定的Stack类对象是否等于另一个Stack类对象。此方法位于System.Collections命名空间下。

句法:

public virtual bool Equals (object obj);

此处, obj是要与当前对象进行比较的对象。

返回值:如果指定对象等于当前对象,则此方法返回true,否则返回false。

下面的程序说明了上述方法的用法:

范例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));
    }
}
输出:
True

范例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));
    }
}
输出:
False
True