Free mag vol1 | Page 487

CHAPTER 11  ADVANCED C# LANGUAGE FEATURES public override string ToString() { StringBuilder builder = new StringBuilder(); builder.Append("{ Color = "); builder.Append(this.i__Field); builder.Append(", Make = "); builder.Append(this.i__Field); builder.Append(", CurrentSpeed = "); builder.Append(this.i__Field); builder.Append(" }"); return builder.ToString(); } The GetHashCode() implementation computes a hash value using each anonymous type’s member variables as input to the System.Collections.Generic.EqualityComparer type. Using this implementation of GetHashCode(), two anonymous types will yield the same hash value if (and only if) they have the same set of properties that have been assigned the same values. Given this implementation, anonymous types are well-suited to be contained within a Hashtable container. The Semantics of Equality for Anonymous Types While the implementation of the overridden ToString() and GetHashCode() methods is fairly straightforward, you might be wondering how the