HashSet or Distinct to read distinct values of property in List<> of objects

与世无争的帅哥 提交于 2019-12-01 22:24:50

The HashSet one, since it keeps the objects around after the hashset object has been constructed, and foreach-ing it will not require expensive operations.

On the other hand, the Distinct enumerator will likely be evaluated every time the DataSource is enumerated, and all the work of removing duplicate values will be repeated.

If it is a one time operation - use .Distinct. If you are going to add elements again and again - use HashSet

Liam

Being as their are no better answers coming forward I'm going to answer my own question. I decided to use a HashSet though the performance benefits are possibly marginal for my size of result set. When it comes down to it the definition of a HashSet taken from here was:

HashSet is an unordered collection containing unique elements.

And I wanted an unordered collection of values that were unique. (Horses for courses) I also do not require indexing and I only wish to enumerate my set.

So it seemed the best fit. Marking Staffi as the answer as his was the most informative though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!