C# 自定义数据类型的非唯一元素
我正在尝试获取自定义数据类型的唯一元素列表。我真的不明白为什么这不起作用。在下面的代码中,控件永远不会到达Equals实现。有人能帮忙吗C# 自定义数据类型的非唯一元素,c#,C#,我正在尝试获取自定义数据类型的唯一元素列表。我真的不明白为什么这不起作用。在下面的代码中,控件永远不会到达Equals实现。有人能帮忙吗 public class customobj : IEqualityComparer<customobj> { public string str1; public string str2; public customobj(string s1, string s2) { this.str1 = s
public class customobj : IEqualityComparer<customobj>
{
public string str1;
public string str2;
public customobj(string s1, string s2)
{
this.str1 = s1; this.str2 = s2;
}
public bool Equals(customobj obj1, customobj obj2)
{
if ((obj1 == null) || (obj2 == null))
{
return false;
}
return ((obj1.str1.Equals(obj2.str1)) && (obj2.str2.Equals(obj2.str2)));
}
public int GetHashCode(customobj w)
{
if (w != null)
{
return ((w.str1.GetHashCode()) ^ (w.str2.GetHashCode()));
}
return 0;
}
}
公共类customobj:IEqualityComparer
{
公共字符串str1;
公共字符串str2;
公共customobj(字符串s1、字符串s2)
{
this.str1=s1;this.str2=s2;
}
公共布尔等于(customobj obj1、customobj obj2)
{
如果((obj1==null)| |(obj2==null))
{
返回false;
}
返回((obj1.str1.Equals(obj2.str1))&(obj2.str2.Equals(obj2.str2));
}
public int GetHashCode(customobj w)
{
如果(w!=null)
{
返回((w.str1.GetHashCode())^(w.str2.GetHashCode());
}
返回0;
}
}
下面是我试图检索列表中不同元素的部分
List<customobj> templist = new List<customobj> { };
templist.Add(new customobj("10", "50"));
templist.Add(new customobj("10", "50"));
List<customobj> dist = templist.Distinct().ToList();
List templast=新列表{};
添加(新customobj(“10”,“50”));
添加(新customobj(“10”,“50”));
List dist=templast.Distinct().ToList();
您的类不会重写对象类中的base Equals(),而Distinct()
正在使用它
尝试覆盖基本相等,并从那里调用自定义的相等(矩形obj1、矩形obj2)
另外,如果要从类型化比较器继承,请使用
IEquatable
,但不要使用IEqualityComparer
您的类不会重写对象类中的基等于(),并且Distinct()
正在使用它
尝试覆盖基本相等,并从那里调用自定义的相等(矩形obj1、矩形obj2)
此外,如果要从类型化比较器继承,请使用IEquatable
,但不要使用IEqualityComparer
是对象的静态方法,因此无法重写
您必须替代实例Equals
public override bool Equals(Object obj) {
...
}
是对象的静态方法,因此无法重写
您必须替代实例Equals
public override bool Equals(Object obj) {
...
}
如果您想在Rectangle的类中实现IEqualityComparer,您应该编写以下内容:
List<Rectangle> dist = templist.Distinct(new Reclangle("","")).ToList();
List dist=templast.Distinct(新的倾角(“,”).ToList();
通常通过矩形比较器类实现:
class RectangleComparer : IEqualityComparer<Rectangle>
{
public static IEqualityComparer<Rectangle> Instance { get {...} }
...
}
List<Rectangle> dist = templist.Distinct(RectangleComparer.Instance).ToList();
类矩形比较器:IEqualityComparer
{
公共静态IEqualityComparer实例{get{…}
...
}
List dist=templast.Distinct(RectangleComparer.Instance.ToList();
或者重写GetHashCode和Equals=)如果您想在Rectangle的类中实现IEqualityComparer,您应该编写以下内容:
List<Rectangle> dist = templist.Distinct(new Reclangle("","")).ToList();
List dist=templast.Distinct(新的倾角(“,”).ToList();
通常通过矩形比较器类实现:
class RectangleComparer : IEqualityComparer<Rectangle>
{
public static IEqualityComparer<Rectangle> Instance { get {...} }
...
}
List<Rectangle> dist = templist.Distinct(RectangleComparer.Instance).ToList();
类矩形比较器:IEqualityComparer
{
公共静态IEqualityComparer实例{get{…}
...
}
List dist=templast.Distinct(RectangleComparer.Instance.ToList();
或者重写GetHashCode和Equals=)您实现了错误的接口。您的类实现了
IEqualityComparer
,而不是IEquatable
。除非您将IEqualityComparer
传递给Distinct
,否则它将使用IEquatable.Equals
(如果您的类实现了它)或对象.Equals
public class Rectangle : IEquatable<Rectangle>
{
public string width;
public string height;
public Rectangle(string s1, string s2)
{
this.width = s1; this.height = s2;
}
`IEquatable.Equals
public bool Equals(Rectangle obj2)
{
if (obj2 == null)
{
return false;
}
return ((this.width.Equals(obj2.width)) && (this.height.Equals(obj2.height)));
}
`override of object.Equals
public override bool Equals(Object(o2)
{
if(typeof(o2) == typeof(Rectangle))
return ((Rectangle)this.Equals((Rectangle)o2);
return false;
}
'override of object.GetHashCode
public override int GetHashCode()
{
return ((this.width.GetHashCode()) ^ (thisw.height.GetHashCode()));
}
}
公共类矩形:IEquatable
{
公共字符串宽度;
公共线高度;
公共矩形(字符串s1、字符串s2)
{
this.width=s1;this.height=s2;
}
`相等
公共布尔等于(矩形obj2)
{
如果(obj2==null)
{
返回false;
}
返回((this.width.Equals(obj2.width))&&(this.height.Equals(obj2.height));
}
`对象的重写。等于
公共覆盖布尔等于(对象(o2)
{
如果(类型(o2)=类型(矩形))
返回((矩形)this.Equals((矩形)o2);
返回false;
}
'重写object.GetHashCode
公共覆盖int GetHashCode()
{
返回((this.width.GetHashCode())^(thisw.height.GetHashCode());
}
}
另外,您的
宽度
和高度
是字符串
而不是数字类型,这有什么特别的原因吗?它看起来很奇怪,可能会导致奇怪的错误,例如假设“100”
和“0100”
和“100”
是相等的,而实际上它们是不同的字符串,并且将具有不同的哈希代码。您实现了错误的接口。您的类实现了IEqualityComparer
,而不是IEquatable
。除非您将IEqualityComparer
传递给distinct
,否则它将使用IEquatable.Equals>
(如果您的类实现了它)或对象。等于
public class Rectangle : IEquatable<Rectangle>
{
public string width;
public string height;
public Rectangle(string s1, string s2)
{
this.width = s1; this.height = s2;
}
`IEquatable.Equals
public bool Equals(Rectangle obj2)
{
if (obj2 == null)
{
return false;
}
return ((this.width.Equals(obj2.width)) && (this.height.Equals(obj2.height)));
}
`override of object.Equals
public override bool Equals(Object(o2)
{
if(typeof(o2) == typeof(Rectangle))
return ((Rectangle)this.Equals((Rectangle)o2);
return false;
}
'override of object.GetHashCode
public override int GetHashCode()
{
return ((this.width.GetHashCode()) ^ (thisw.height.GetHashCode()));
}
}
公共类矩形:IEquatable
{
公共字符串宽度;
公共线高度;
公共矩形(字符串s1、字符串s2)
{
this.width=s1;this.height=s2;
}
`相等
公共布尔等于(矩形obj2)
{
如果(obj2==null)
{
返回false;
}
返回((this.width.Equals(obj2.width))&&(this.height.Equals(obj2.height));
}
`对象的重写。等于
公共覆盖布尔等于(对象(o2)
{
如果(类型(o2)=类型(矩形))
返回((矩形)this.Equals((矩形)o2);
返回false;
}
'重写object.GetHashCode
公共覆盖int GetHashCode()
{
返回((this.width.GetHashCode())^(thisw.height.GetHashCode());
}
}
另外,您的宽度
和高度
是字符串
而不是数字类型,这有什么特别的原因吗?这看起来很奇怪,可能会导致奇怪的错误,比如假设“100”
和“0100”
和“100”
是相等的