结构比较
数组和元组都实现接口IStructuralEquatable和IStructuralComparable.这两个接口不仅可以比较引用,还可以比较内容.这些接口都是显示实现的,所以在使用时需要把数组和元组强制转换为这个接口.IStructuralEquatable接口用于比较两个元组或数组是否有相同的内容,IStructuralComparable接口用于给元组或数组排序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 结构比较
{
class Program
{
static void Main(string[] args)
{
//创建两个Person项的数组.
//比较运算符!=返回true
//因为这其实是两个变量p1和p2引用的两个不同数组.
//因为array类没有重写带一个参数的Equals()放大,所以用"=="运算符
//比较引用会得到相同的结构,即这两个变量不相同
Person zhangsan = new Person { FirstName = "zhang", LastName = "san" };
Person[] p1 = {
new Person
{
FirstName="lisi"
} ,
zhangsan
};
Person[] p2 = {
new Person
{
FirstName="lisi"
} ,
zhangsan
};
if (p1!=p2)
{
Console.WriteLine("not the same reference"); ;
}
Console.ReadKey();
}
}
public class Person : IEquatable<Person>
{
public int ID { get; set; }
public string FirstName { set; get; }
public string LastName { set; get; }
public override string ToString()
{
return string.Format("{0},{1}{2}", ID, FirstName, LastName); ;
}
public override bool Equals(object obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
return Equals(obj as Person);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
public bool Equals(Person other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
return this.ID == other.ID && this.FirstName == other.FirstName && this.LastName == other.LastName;
}
}
}
使用实现IEquatable接口的Person类.IEquatable接口定义了一个强类型化的Equals()方法,用来比较FirstName和LastName属性的值.
下面看看如何对元组执行相同的操作.这里创建了两个内容相同的元组实例:
var t1 = Tuple.Create<string, int>("zhangsan", 19);
var t2 = Tuple.Create<string, int>("zhangsan", 19);
//因为t1和t2引用了两个不同的对象,所以比较运算符"!="返回true
if (t1 != t2)
{
Console.WriteLine("not the same reference to the tuple");
}
//这会调用object.equals()方法比较元组的每一项,每一项都返回true
if (t1.Equals(t2))
{
Console.WriteLine("the same reference to the tuple");
}
Tuple<>类提供了两个Equals()方法:一个重写了object基类中的Equals()方法,并把object作为参数,第二个由IStructyralEqualityComparer接口定义,并把object和IequalityComparer作为参数.
还可以使用类TupleCOmparer创建一个自定义的UequalityComparer,这个类实现了IEqualityComparer接口的两个方法Equals()和GetHashCode():
public class TupleComparer : IEqualityComparer
{
public bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}
实现IEqualityCOmparer接口的Equals()方法需要new修饰符或者隐式实现的接口,因为基类object也定义了带两个参数的静态的Equals()方法.
使用TupleComparer,给Tuple<T1,T2>类的Equals()方法传递一个新实例.Tuple类的Equals(0方法为要比较的每一项调用TupleComparer的Equals()方法.所以,对于Tuple<T1,T2>类,要调用两次TupleCOmparer,以检查所有项是否相等.