13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Beyond the basics83derive from a particular type that overloads the == and != operators, those overloads areused. Beware, however—extra overloads that happen to be made available by the typeargument specified by the caller are not used. List<strong>in</strong>g 3.5 demonstrates this with a simplereference type constra<strong>in</strong>t and a type argument of str<strong>in</strong>g.List<strong>in</strong>g 3.5Comparisons us<strong>in</strong>g == and != us<strong>in</strong>g reference comparisonsstatic bool AreReferencesEqual (T first, T second)where T : class{B Comparesreturn first==second;references}...str<strong>in</strong>g name = "Jon";str<strong>in</strong>g <strong>in</strong>tro1 = "My name is "+name;str<strong>in</strong>g <strong>in</strong>tro2 = "My name is "+name;Console.WriteL<strong>in</strong>e (<strong>in</strong>tro1==<strong>in</strong>tro2);Console.WriteL<strong>in</strong>e (AreReferencesEqual(<strong>in</strong>tro1, <strong>in</strong>tro2));Even though str<strong>in</strong>g overloads == (as demonstrated by C pr<strong>in</strong>t<strong>in</strong>g True), this overloadis not used by the comparison at B. Basically, when AreReferencesEqual is compiledthe compiler doesn’t know what overloads will be available—it’s as if the parameterspassed <strong>in</strong> were just of type object.This is not just specific to operators—when the compiler encounters ageneric type, it resolves all the method overloads when compil<strong>in</strong>gthe unbound generic type, rather than reconsider<strong>in</strong>g each possiblemethod call for more specific overloads at execution time. For <strong>in</strong>stance, astatement of Console.WriteL<strong>in</strong>e (default(T)); will always resolve to callConsole.WriteL<strong>in</strong>e(object value)—it doesn’t call Console.WriteL<strong>in</strong>e(str<strong>in</strong>g value) when T happens to be str<strong>in</strong>g. This is similar to the normalsituation of overloads be<strong>in</strong>g chosen at compile time rather than executiontime, but readers familiar with templates <strong>in</strong> C++ may be surprised nonetheless.Two classes that are extremely useful when it comes to compar<strong>in</strong>g values are Equality-Comparer and Comparer, both <strong>in</strong> the System.Collections.Generic namespace.They implement IEqualityComparer (useful for compar<strong>in</strong>g and hash<strong>in</strong>g dictionarykeys) and IComparer (useful for sort<strong>in</strong>g) respectively, and the Default propertyreturns an implementation that generally does the right th<strong>in</strong>g for the appropriate type.See the documentation for more details, but consider us<strong>in</strong>g these (and similar typessuch as Str<strong>in</strong>gComparer) when perform<strong>in</strong>g comparisons. We’ll use Equality-Comparer <strong>in</strong> our next example.Caution!Possiblyunexpectedbehavior!Compares us<strong>in</strong>gstr<strong>in</strong>g overloadFULL COMPARISON EXAMPLE: REPRESENTING A PAIR OF VALUESTo f<strong>in</strong>ish off our section on implement<strong>in</strong>g generics—and <strong>in</strong>deed “medium-level” generics—here’sa complete example. It implements a useful generic type—a Pair, which just holds two values together, like a key/value pair, but withno expectations as to the relationship between the two values. As well as provid<strong>in</strong>g propertiesto access the values themselves, we’ll override Equals and GetHashCode to allowCLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!