When should I use the HashSet type?

前端 未结 11 1061
温柔的废话
温柔的废话 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 03:10

    HashSet would be used to remove duplicate elements in an IEnumerable collection. For example,

    List duplicatedEnumrableStrings = new List {"abc", "ghjr", "abc", "abc", "yre", "obm", "ghir", "qwrt", "abc", "vyeu"};
    HashSet uniqueStrings = new HashSet(duplicatedEnumrableStrings);
    

    after those codes are run, uniqueStrings holds {"abc", "ghjr", "yre", "obm", "qwrt", "vyeu"};

提交回复
热议问题