Distinct

suggest change

Returns unique values from an IEnumerable. Uniqueness is determined using the default equality comparer.

int[] array = { 1, 2, 3, 4, 2, 5, 3, 1, 2 };

var distinct = array.Distinct();
// distinct = { 1, 2, 3, 4, 5 }

Distinct uses EqualityComparer<T>.Default, which calls IEquatable<T>.Equals when the type implements it, and GetHashCode. You can pass a comparer instead:

class SSNEqualityComparer : IEqualityComparer<Person> {
    public bool Equals(Person a, Person b) => a.SSN == b.SSN;
    public int GetHashCode(Person p) => p.SSN;
}

List<Person> people;

var distinct = people.Distinct(new SSNEqualityComparer());

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents