Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C# HashSets have a Distinct() method

Tags:

c#

hashset

Why is there a Distinct() method available on the HashSet when they cannot contain duplicates anyway?

like image 429
atlantis Avatar asked Jun 07 '12 14:06

atlantis


People also ask

Why do we do C programming?

C programming language uses blocks to separate pieces of code performing different tasks. This helps make programming easier and keeps the code clean. Thus, the code is easy to understand even for those who are starting out. C is used in embedded programming, which is used to control micro-controllers.

Why is %d used in C?

%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value. ( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example.


1 Answers

The Distinct method is not on the HashSet<>, but the IEnumerable<> that is implemented by the HashSet<>.

Extension methods cannot be "omitted" from certain types. Once added to a type, all of that type and any derived will get the extension method.

Just to demonstrate, if you extended object you'd litter everything if you added the relevant namespace. So don't go adding:

namespace System
{
    public static class ObjectExtensions
    {
        public static void Garbage(this object foo)
        {
        }
    }
}
like image 157
Adam Houldsworth Avatar answered Oct 05 '22 21:10

Adam Houldsworth