mphillips (3) [Avatar] Offline
#1
Could you please explain what the .Default property is suppose to accomplish here? It thought we wanted to perform comparisons between TFirsts and comparisons between TSeconds not their Default values. Does it provide the default value if one is missing ?

Book Code:

return EqualityComparer<TFirst>.Default
.Equals(this.First, other.First) &&
EqualityComparer<TSecond>.Default
.Equals(this.Second, other.Second);

Thanks
jon.skeet (448) [Avatar] Offline
#2
Re: Chapter 3: Listing 3.6 Default in Generic Pair Equals
It's the default equality comparer, not a default value for TFirst or TSecond. It basically uses the most appropriate equality comparison available, based on the nongeneric and generic interfaces.

It may be clearer if we rewrite it as:

EqualityComparer<TFirst> firstComparer = EqualityComparer<TFirst>.Default;
EqualityComparer<TFirst> secondComparer = EqualityComparer<TSecond>.Default;

return firstComparer.Compare(this.First, other.First) && secondComparer.Comparer(this.Second, other.Second);

Does that make it any clearer?
mgravell (64) [Avatar] Offline
#3
Re: Chapter 3: Listing 3.6 Default in Generic Pair Equals
typo in second line:

EqualityComparer<TSecond> secondComparer = EqualityComparer<TSecond>.Default;