The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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 (483) [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;