C# - List.Remove() 始终删除列表中的第一个对象
在 Visual Studio 2008 (C#) 中工作... 我使用 List 集合来存储自定义类 (Shift) 的实例。
我想使用“删除”方法从列表中删除某个班次。
但 List.Remove() 总是删除它找到的第一个项目。
我已经为我的 Shift 实现了 IComparable 接口,我认为这已经足够了,然后我添加了 IEqualityComparer 的实现,但它仍然没有效果。
这是我的实现的摘录:
地区I可比会员
public int CompareTo(object obj) { 移位 s1 = 这个; Shift s2 = (Shift)obj; if (s1.开始时间!= s2.开始时间) 返回 s1.start.CompareTo(s2.start); 别的 返回 s1.end.CompareTo(s2.end); }
结束区域
地区 IEqualityComparer 成员
public bool 等于(Shift x, Shift y) { if ((x.opening) != (y.opening)) 返回 false; if ((x.ending) != (y.ending)) 返回 false; if (!x.opening) if (x._start != y._start) return false; if (!x.close) if (x._end != y._end) return false; if (x.when != y.when) 返回 false; if (x.day!= y.day) 返回 false; if (x.EmployeeID != y.EmployeeID) 返回 false; 返回真; } 公共 int GetHashCode(Shift obj) { 返回 obj.ToString().ToLower().GetHashCode(); }
结束区域
然而,仍然 - 当列表包含两个班次时,说“8:00 - 15:00”; “12:00 - 16:00”,调用Remove(“12:00-16:00”)会导致“8:00 - 15:00”被删除,而后一个仍保留在集合中!
这是怎么回事?谢谢
Working in Visual Studio 2008 (C#)...
I use a List collection to store instances of my custom class (Shift).
I want to delete a certain shift from the list by using the Remove method.
But List.Remove() always deletes the first item it finds.
I've implemented the IComparable interface for my Shift, I thought this would be enough, then I added an implementation of IEqualityComparer, and it still has no effect.
Here's the excerpt with my implementation:
region IComparable Members
public int CompareTo(object obj) { Shift s1 = this; Shift s2 = (Shift)obj; if (s1.start.time != s2.start.time) return s1.start.CompareTo(s2.start); else return s1.end.CompareTo(s2.end); }
endregion
region IEqualityComparer Members
public bool Equals(Shift x, Shift y) { if ((x.opening) != (y.opening)) return false; if ((x.closing) != (y.closing)) return false; if (!x.opening) if (x._start != y._start) return false; if (!x.closing) if (x._end != y._end) return false; if (x.when != y.when) return false; if (x.day != y.day) return false; if (x.EmployeeID != y.EmployeeID) return false; return true; } public int GetHashCode(Shift obj) { return obj.ToString().ToLower().GetHashCode(); }
endregion
And yet, still - when the List contains two shifts, say "8:00 - 15:00"; "12:00 - 16:00", calling Remove("12:00-16:00") results in "8:00 - 15:00" getting removed, and the latter one remains in the collection!
What's wrong here? Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以覆盖
object.GetHashCode
和object.Equals
:您还应该在
Equals(x, y)
中执行 null 检查。You can override
object.GetHashCode
andobject.Equals
:You should also probably do a null check in
Equals(x, y)
.IComparable
通常不用于比较相等性(它用于排序),因此List.Remove()
会忽略它。出于平等目的,
IEqualityComparer
并不等同于IComparable
。它应该由一个比较器对象实现 - 即比较其他对象是否相等的对象。如果您希望相等比较是您的类所固有的,那么您需要实现IEquatable
。或者只是在类上重写Object.Equals()
和Object.GetHashCode()
,而不实现任何接口。IComparable
is not normally used to compare for equality (it's used for ordering), soList<T>.Remove()
ignores it.IEqualityComparer
is not an equivalent ofIComparable
for equality purposes. It is supposed to be implemented by a comparer object - that is, an object that compares other objects for equality. If you want equality comparisons to be inherent to your class, then you rather need to implementIEquatable<T>
. Or just overrideObject.Equals()
andObject.GetHashCode()
on your class, without implementing any interfaces.删除使用
EqualityComparer.Default
来确定相等性并选择要删除的对象,如果在您的对象上实现,则将使用IEquatable
,否则,它将使用引用相等。您有两种选择来获得所需的行为:
1) 让 Shift 实现
IEquatable
(不仅仅是重写 Object.Equals 或创建方法,而是创建 Shift -Shift : IEquatable
)2) 使用
List.RemoveAt
Remove uses
EqualityComparer<T>.Default
to determine equality and choose which object to remove, which will useIEquatable<T>
if it's implemented on your object, otherwise, it will use reference equality.You have two options to get the behavior you want:
1) Make Shift implement
IEquatable<T>
(not just override Object.Equals or make the method, but make Shift -Shift : IEquatable<Shift>
)2) Use
List<T>.RemoveAt
根据您提供的示例,您正在调用:
"12:00 - 16:00"
在本例中,是一个String
值,而不是实际的Shift对象。确保您正在编写的
CompareTo
方法中正确地将String
值转换为Shift
对象。否则,当比较开始时间时……事情可能会变得混乱。With the example you provided, you're calling:
"12:00 - 16:00"
in this case, is aString
value and not an actualShift
object. Make sure that in yourCompareTo
method that you're code is properly casting theString
value to aShift
object. Otherwise, when it compares start times...things could go haywire.