从Object类继承的Equals(Object)方法用于检查指定的List
句法:
public virtual bool Equals (object obj);
此处, obj是要与当前对象进行比较的对象。
返回值:如果指定对象等于当前对象,则此方法返回true ,否则返回false 。
下面的程序说明了上面讨论的方法的使用:
范例1:
// C# program to if a List object
// is equal to another List object
using System;
using System.Collections.Generic;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Creating an List of Integers
List firstlist = new List();
// Adding elements to List
firstlist.Add(17);
firstlist.Add(19);
firstlist.Add(21);
firstlist.Add(9);
firstlist.Add(75);
firstlist.Add(19);
firstlist.Add(73);
// Checking whether firstlist is
// equal to itself or not
Console.WriteLine(firstlist.Equals(firstlist));
}
}
输出:
True
范例2:
// C# program to if a List object
// is equal to another List object
using System;
using System.Collections.Generic;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Creating a List of strings
List list1 = new List();
// Inserting elements in List
list1.Add("DS");
list1.Add("C++");
list1.Add("Java");
list1.Add("JavaScript");
// Creating an List of Integers
List list2 = new List();
// Adding elements to List
list2.Add(78);
list2.Add(44);
list2.Add(27);
list2.Add(98);
list2.Add(74);
// Checking whether list1 is
// equal to list2 or not
Console.WriteLine(list1.Equals(list2));
// Creating a List of integers
List list3 = new List();
// Assigning list2 to list3
list3 = list2;
// Checking whether list3 is
// equal to list2 or not
Console.WriteLine(list3.Equals(list2));
}
}
输出:
False
True
注意:如果当前实例是引用类型,则Equals(Object)方法将检查引用是否相等。