13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced generics85implement IEquatable to give a strongly typed API that willavoid unnecessary execution time checks. The equality and hash-code computationsboth use the default equality comparer for the two type parameters—these handlenulls for us automatically, which makes the code somewhat simpler. 5If we wanted to support sort<strong>in</strong>g, we could implement IComparer , perhaps order<strong>in</strong>g by the first component and then the second.This k<strong>in</strong>d of type is a good candidate for bear<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d what functionality you mightwant, but not actually implement<strong>in</strong>g until you need it.We’ve f<strong>in</strong>ished look<strong>in</strong>g at our “<strong>in</strong>termediate” features now. I realize it can all seemcomplicated at first sight, but don’t be put off: the benefits far outweigh the addedcomplexity. Over time they become second nature. Now that you’ve got the Pair classas an example, it might be worth look<strong>in</strong>g over your own code base to see whetherthere are some patterns that you keep reimplement<strong>in</strong>g solely to use different types.With any large topic there is always more to learn. The next section will take youthrough the most important advanced topics <strong>in</strong> generics. If you’re feel<strong>in</strong>g a bit overwhelmedby now, you might want to skip to the relative comfort of section 3.5, where weexplore the generic collections provided <strong>in</strong> the framework. It’s well worth understand<strong>in</strong>gthe topics <strong>in</strong> the next section eventually, but if everyth<strong>in</strong>g so far has been new to youit wouldn’t hurt to skip it for the moment.3.4 Advanced genericsYou may be expect<strong>in</strong>g me to claim that <strong>in</strong> the rest of this chapter we’ll be cover<strong>in</strong>gevery aspect of generics that we haven’t looked at so far. However, there are so many littlenooks and crannies <strong>in</strong>volv<strong>in</strong>g generics, that’s simply not possible—or at least, I certa<strong>in</strong>lywouldn’t want to even read about all the details, let alone write about them.Fortunately the nice people at Microsoft and ECMA have written all the details <strong>in</strong> thefreely available language specification, 6 so if you ever want to check some obscure situationthat isn’t covered here, that should be your next port of call. Arguably if yourcode ends up <strong>in</strong> a corner case complicated enough that you need to consult the specificationto work out what it should do, you should refactor it <strong>in</strong>to a more obvious formanyway; you don’t want each ma<strong>in</strong>tenance eng<strong>in</strong>eer from now until eternity to have toread the specification.My aim with this section is to cover everyth<strong>in</strong>g you’re likely to want to know aboutgenerics. It talks more about the CLR and framework side of th<strong>in</strong>gs than the particularsyntax of the <strong>C#</strong> 2 language, although of course it’s all relevant when develop<strong>in</strong>g<strong>in</strong> <strong>C#</strong> 2. We’ll start by consider<strong>in</strong>g static members of generic types, <strong>in</strong>clud<strong>in</strong>g type <strong>in</strong>itialization.From there, it’s a natural step to wonder just how all this is implemented5The formula used for calculat<strong>in</strong>g the hash code based on the two “part” results comes from read<strong>in</strong>g EffectiveJava (Prentice Hall PTR, 2001) by Joshua Bloch. It certa<strong>in</strong>ly doesn’t guarantee a good distribution of hashcodes, but <strong>in</strong> my op<strong>in</strong>ion it’s better than us<strong>in</strong>g a bitwise exclusive OR. See Effective Java for more details, and<strong>in</strong>deed for many other useful tips.6http://www.ecma-<strong>in</strong>ternational.org/publications/standards/Ecma-334.htmLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!