atlantis
atlantis

Reputation: 3126

Why do C# HashSets have a Distinct() method

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

Upvotes: 6

Views: 1328

Answers (1)

Adam Houldsworth
Adam Houldsworth

Reputation: 64467

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)
        {
        }
    }
}

Upvotes: 13

Related Questions