Arthur Petit
6 February 2025
Understanding Why Distinct() Doesn't Call Equals in .NET 8

Developers frequently assume that while working with Distinct() in C#, it will call Equals() and GetHashCode(), however this isn't always the case. The default behavior may not work as intended if an object, like a list, has a IEnumerable attribute. This article explains why this occurs and offers fixes, such as using a custom IEqualityComparer and IEquatable. You can make sure that separate filtering functions as intended by appropriately overriding these techniques. These methods will assist you in effectively getting rid of duplicate data, whether you're working with database queries or API answers.