让Dictionary key 支持自定义对象,则对象必须继承IEquatable,重写GetHashCode、Equals
class Foo : IEquatable<Foo> {
public string Name { get; set;}
public int FooID {get; set;}
public override int GetHashCode() {
return Name.GetHashCode()+FooID.GetHashCode();
}
public override bool Equals(object obj) {
return Equals(obj as Foo);
}
public bool Equals(Foo obj) {
return obj != null && obj.FooID == this.FooID;
}
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2012/06/04/2535153.html,如需转载请自行联系原作者