📜  C#|检查两个SortedList对象是否相等

📅  最后修改于: 2021-05-29 17:38:53             🧑  作者: Mango

从Object类继承的Equals(Object)方法用于检查指定的SortedList对象是否等于另一个SortedList对象。

句法:

public virtual bool Equals (object obj);

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

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

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to if a SortedList
// is equal to another SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a sorted list of key/value pairs
        SortedList fslist = new SortedList();
  
        // Adding pairs to fslist
        fslist.Add("Maths ", 98);
        fslist.Add("English ", 99);
        fslist.Add("Physics ", 97);
        fslist.Add("Chemistry", 96);
        fslist.Add("CSE     ", 100);
  
        // Checking whether fslist is
        // equal to itself or not
        Console.WriteLine(fslist.Equals(fslist));
    }
}

输出:

True

范例2:

// C# program to if a SortedList
// is equal to another SortedList
using System;
using System.Collections;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a sorted list of key/value pairs
        SortedList fslist = new SortedList();
  
        // Adding pairs to fslist
        fslist.Add("Maths ", 98);
        fslist.Add("English ", 99);
        fslist.Add("Physics ", 97);
        fslist.Add("Chemistry", 96);
        fslist.Add("CSE     ", 100);
  
        // Creating an SortedList
        SortedList fslist2 = new SortedList();
  
        // Adding elements to SortedList
        fslist2.Add("1", "one");
        fslist2.Add("2", "two");
        fslist2.Add("3", "three");
        fslist2.Add("4", "four");
        fslist2.Add("5", "five");
  
        // Checking whether fslist is
        // equal to fslist2 or not
        Console.WriteLine(fslist.Equals(fslist2));
          
        // Creating a sorted list of key/value pairs
        SortedList fslist3 = new SortedList();
          
        // Assigning fslist2 to fslist3
        fslist3 = fslist2;
          
        // Checking whether fslist3 is
        // equal to fslist2 or not
        Console.WriteLine(fslist3.Equals(fslist2));    
    }
}

输出:

False
True

注意:如果当前实例是引用类型,则Equals(Object)方法将检查引用是否相等。