集合行为中奇怪的 C# 结构
关于为什么会这样的任何想法
public Collection<Point> data = new Collection<Point>(){
new Point{X=10,Y=20},
new Point{X=20,Y=30},
new Point{X=40,Y=20},
new Point{X=10,Y=20}
};
(注意相同的第一个和最后一个元素) 给出错误
已添加具有相同键的项目。
如果将最后一个元素更改为 Y=20.1 或任何使其不同的内容,则它会起作用。 您还可以按照自己喜欢的方式添加元素并获得相同的结果。
问题显然是由于 Point 是一种值类型,因为如果您定义并使用点类,它就会消失,并且我知道在其他集合类型中使用结构存在问题,但这与值和引用返回之间的差异有关类型。让我感到困惑的是,如果所有结构都具有不同的字段值,那么这会起作用。
Any ideas as to why this
public Collection<Point> data = new Collection<Point>(){
new Point{X=10,Y=20},
new Point{X=20,Y=30},
new Point{X=40,Y=20},
new Point{X=10,Y=20}
};
(notice the identical first and last elements)
gives the error
An item with the same key has already been added.
If you change the last element to Y=20.1 or anything that makes it different then it works.
Also you can add the elements anyway you like and get the same result.
The problem is obviously due to Point being a value type because it goes away if you define and use a point class and I know that there are problems with using structs in other collection types but this has to do with the difference between value and ref return types. What mystifies me is that this works if the all the structs have different field values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
原因是因为值类型的相等性基于其值 - 对于结构体来说,它是其所有字段之间的相等性。
引用类型相等性基于引用本身,因此有效。将结构值更改为完全不同也是可行的。
如果您只想要一个内容列表,只需使用
List
,我认为它会接受重复项。更新:您的集合类似乎正在检测重复的条目,并且您正在尝试添加重复项。如果你想添加重复项,我会说你不能使用这个类。
The reason is because equality of a value type is based on its values - for struct it is equality across all its fields.
Reference type equality is based on the reference itself and thus works. Changing the struct values to be all different also works.
If you just want a list of stuff, just use
List<Point>
, I think that will accept duplicates.Update: it looks like your collection class is detecting duplicate entries and you are trying to add duplicates. If you want to add duplicates, I would say you cannot use this class.
您是否尝试过使用列表来代替?我认为应该有效!
希望有帮助!
Have you tried using a List instead? I think it should work!
Hope that helps!
我不熟悉您正在使用的这个集合类,但显然它不允许其中包含多个项目。就像 SET 集合一样。所以我猜你正在使用的 Collection 相当于:
但由于你没有键,所以它更像是
就像你的集合类一样,HashSet 要求所有键是唯一 >。就像 Kieren 提到的,List 会更适合你。列表允许多个条目相同。
事实上,如果 Point 是一个类,那么它会允许重复,因为 Objec1 != Object2 到即使它们的值相同。
I'm not familiar with this collection class you're using but apparently it doesn't allow multiple items to be in it. As it is with a SET collection. So I guess the Collection you're using is equivalent to:
but since you you dont have a key it's more like
Just like your collection class a HashSet requires all keys to be unique. Like Kieren mentions a List would be more suitable for you. A list allows multiple entries to be the same.
Indeed if Point was a class it would allow duplicates since Objec1 != Object2 to even if their values are the same.
我真的不确定这一点,但我有一种感觉,因为编译器生成一个强大的集合项,不需要装箱/取消装箱值类型,所以关键检查是在显式值类型本身上完成的,这会产生重复键异常?
这真的只是黑暗中的一枪!
I'm really not sure on this, but I have a feeling that because the compiler generates a strong Collections item that doesn't require to box/unbox value types, the key check is done on the explicit value type itself, which produces the duplicate key exception?
That's really just a shot in the dark!
什么是
Collection
类。它不是 .NET Framework 库类。查看此类的文档或来源,它可以解释问题。What is
Collection
class. It's not .NET Framework library class. Look docs or sources of this class, it would explain the problem.