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.
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.