Do collections that prevent inserting the dupl开发者_如何学Goicate elements work slower(than the non-checking ones), as I guess they implement some kind of check on each element within against duplication?
Or it is not correct or tolerable in most of the cases?
Thanks
It depends on the implementation of course, but most sets are likely to be optimised in some form to check for containment quickly. For example, HashSet<T>
is basically a hash table of values - so it's just a hash lookup.
I don't know of any collections which would check every existing element for equality (unless you have a horrible hash collision situation etc).
That totally depends on the implementation of the collection you are using - if it is based on a List<T> there will be a performance penalty.
However, if a HashSet<T> is used, the performance will be almost the same.
Still, performance shouldn't be the motivation here. If you want to allow duplicate items, use a list that does, otherwise use one that doesn't.
精彩评论