When should I use the HashSet type?

前端 未结 11 1045
温柔的废话
温柔的废话 2020-11-28 02:21

I am exploring the HashSet type, but I don\'t understand where it stands in collections.

Can one use it to replace a List

11条回答
  •  情深已故
    2020-11-28 02:51

    In the basic intended scenario HashSet should be used when you want more specific set operations on two collections than LINQ provides. LINQ methods like Distinct, Union, Intersect and Except are enough in most situations, but sometimes you may need more fine-grained operations, and HashSet provides:

    • UnionWith
    • IntersectWith
    • ExceptWith
    • SymmetricExceptWith
    • Overlaps
    • IsSubsetOf
    • IsProperSubsetOf
    • IsSupersetOf
    • IsProperSubsetOf
    • SetEquals

    Another difference between LINQ and HashSet "overlapping" methods is that LINQ always returns a new IEnumerable, and HashSet methods modify the source collection.

提交回复
热议问题