Regarding .Contains ()
The .Contains()
method of List<T>
has a peculiarity which is to use the comparer returned by EqualityComparer.Default. According to the documentation:
The Default property checks whether T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses this implementation. Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
In other words, you can implement the IEqualityComparer<T>
interface in your class or override the base.Equals()
method and this will be the comparison method used.
But why implement IEqualityComparer<T>
?
When you use .Equals()
of the interface, there is a guarantee that the types of the original object and the object to compare are the same. Not only does it make programming safer, it has a slight impact on performance, since, unlike base.Equals()
, boxing / unboxing does not occur in the comparison.
This scenario becomes more important when there is extensive use of comparisons where the costs of boxing / unboxing are no longer negligible.
Any Vs Contain
An interesting analysis is the comparison of complexities between .Any()
and .Contains()
.
.Contains()
is a method of the instance and therefore the complexity is dependent on the collection itself. In the case of a% with% complexity is O (n), while in% with% complexity would be O (1).
Since List<T>
is an extension whose complexity is always O (n), since it will scroll through the entire collection, apply the delegate passed to each element, and return when it finds an element whose delegate returns HashSet
. p>
Finally, the% w / o% due to the delegate is more flexible than the% w / w that only accepts an object.