为什么要实施的IEquatable&LT; T&GT;接口 [英] Why Implement the IEquatable<T> Interface
问题描述
我一直在阅读文章,理解在一定程度上接口但是,如果我想立刻我自己的自定义equals方法,看来我可以做到这一点没有实现IEquatable接口。一个例子。
使用系统;
System.Collections中使用;
使用System.ComponentModel;命名空间ProviderJSONConverter.Data.Components
{
公共类地址:IEquatable&LT;地址&gt;
{
公共字符串的地址{搞定;组; } [默认值()]
公共字符串address_2 {搞定;组; }
公共字符串城市{搞定;组; }
公共字符串状态{搞定;组; }
公共字符串拉链{搞定;组; } 公共布尔等于(地址等)
{
如果(Object.ReferenceEquals(其他,NULL))返回false;
如果(Object.ReferenceEquals(这一点,其他))返回true;
回报(this.address.Equals(other.address)
&功放;&安培; this.address_2.Equals(other.address_2)
&功放;&安培; this.city.Equals(other.city)
&功放;&安培; this.state.Equals(other.state)
&功放;&安培; this.zip.Equals(other.zip));
}
}
}
现在如果我不实现接口,并留下:IEquatable&LT;地址&gt;
出code,似乎应用程序来操作完全一样。因此,我不清楚为什么实现接口?我可以写我自己的自定义equals方法没有它和断点将仍然打的方法和给回相同的结果。
任何人都可以帮助解释这个给我吗?我挂了,为什么有 IEquatable&LT;地址&gt;
之前调用Equals方法
现在如果我不实现接口,并留下:IEquatable了code,似乎该应用程序完全相同的操作与
。
块引用>嗯,这要看是什么应用程序一样。例如:
列表&LT;地址&gt;地址=新的List&LT;地址&gt;
{
新地址 { ... }
};
INT指数= addresses.IndexOf(新地址{...});...这是行不通的(即
首页
将为-1),如果你既没有覆盖等于(对象)
也未执行IEquatable&LT; T&GT;
。列表&LT; T&GT; .IndexOf
不会打电话给你的等于
超载code,它知道你的具体的类将拿起
等于
过载 - 但任何code(如泛型集合所有的LINQ到对象等),这只是工作的任意的对象将不会把它捡起来。I have been reading articles and understand interfaces to an extent however, if i wanted to right my own custom Equals method, it seems I can do this without implementing the IEquatable Interface. An example.
using System; using System.Collections; using System.ComponentModel; namespace ProviderJSONConverter.Data.Components { public class Address : IEquatable<Address> { public string address { get; set; } [DefaultValue("")] public string address_2 { get; set; } public string city { get; set; } public string state { get; set; } public string zip { get; set; } public bool Equals(Address other) { if (Object.ReferenceEquals(other, null)) return false; if (Object.ReferenceEquals(this, other)) return true; return (this.address.Equals(other.address) && this.address_2.Equals(other.address_2) && this.city.Equals(other.city) && this.state.Equals(other.state) && this.zip.Equals(other.zip)); } } }
Now if i dont implement the interface and leave
: IEquatable<Address>
out of the code, it seems the application operates exactly the same. Therefore, I am unclear as to why implement the interface? I can write my own custom Equals method without it and the breakpoint will hit the method still and give back the same results. Can anyone help explain this to me more? I am hung up on why include "IEquatable<Address>
" before calling the Equals method.解决方案Now if i dont implement the interface and leave : IEquatable out of the code, it seems the application operates exactly the same.
Well, that depends on what "the application" does. For example:
List<Address> addresses = new List<Address> { new Address { ... } }; int index = addresses.IndexOf(new Address { ... });
... that won't work (i.e.
index
will be -1) if you have neither overriddenEquals(object)
nor implementedIEquatable<T>
.List<T>.IndexOf
won't call yourEquals
overload.Code that knows about your specific class will pick up the
Equals
overload - but any code (e.g. generic collections, all of LINQ to Objects etc) which just works with arbitrary objects won't pick it up.这篇关于为什么要实施的IEquatable&LT; T&GT;接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!