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.

64 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsFor most people, generics will be the most important new feature of <strong>C#</strong> 2. Theyenhance performance, make your code more expressive, and move a lot of safetyfrom execution time to compile time. Essentially they allow you to parameterize typesand methods—just as normal method calls often have parameters to tell them whatvalues to use, generic types and methods have type parameters to tell them what typesto use. It all sounds very confus<strong>in</strong>g to start with—and if you’re completely new togenerics you can expect a certa<strong>in</strong> amount of head scratch<strong>in</strong>g—but once you’ve gotthe basic idea, you’ll come to love them.In this chapter we’ll be look<strong>in</strong>g at how to use generic types and methods that othershave provided (whether <strong>in</strong> the framework or as third-party libraries), and how to writeyour own. We’ll see the most important generic types with<strong>in</strong> the framework, and take alook just under the surface to understand some of the performance implications ofgenerics. To conclude the chapter, I’ll present some of the most frequently encounteredlimitations of generics, along with possible workarounds, and compare generics<strong>in</strong> <strong>C#</strong> with similar features <strong>in</strong> other languages.First, though, we need to understand the problems that caused generics to bedevised <strong>in</strong> the first place.3.1 Why generics are necessaryHave you ever counted how many casts you have <strong>in</strong> your <strong>C#</strong> 1 code? If you use any ofthe built-<strong>in</strong> collections, or if you’ve written your own types that are designed to workwith many different types of data, you’ve probably got plenty of casts lurk<strong>in</strong>g <strong>in</strong> yoursource, quietly tell<strong>in</strong>g the compiler not to worry, that everyth<strong>in</strong>g’s f<strong>in</strong>e, just treat theexpression over there as if it had this particular type. Us<strong>in</strong>g almost any API that hasobject as either a parameter type or a return type will probably <strong>in</strong>volve casts at somepo<strong>in</strong>t. Hav<strong>in</strong>g a s<strong>in</strong>gle-class hierarchy with object as the root makes th<strong>in</strong>gs morestraightforward, but the object type <strong>in</strong> itself is extremely dull, and <strong>in</strong> order to do anyth<strong>in</strong>ggenu<strong>in</strong>ely useful with an object you almost always need to cast it.Casts are bad, m’kay? Not bad <strong>in</strong> an “almost never do this” k<strong>in</strong>d of way (like mutablestructs and nonprivate fields) but bad <strong>in</strong> a “necessary evil” k<strong>in</strong>d of way. They’re an<strong>in</strong>dication that you ought to give the compiler more <strong>in</strong>formation somehow, and thatthe way you’re choos<strong>in</strong>g is to get the compiler to trust you at compile time and generatea check to run at execution time, to keep you honest.Now, if you need to tell the compiler the <strong>in</strong>formation somewhere, chances are thatanyone read<strong>in</strong>g your code is also go<strong>in</strong>g to need that same <strong>in</strong>formation. They can see itwhere you’re cast<strong>in</strong>g, of course, but that’s not terribly useful. The ideal place to keepsuch <strong>in</strong>formation is usually at the po<strong>in</strong>t of declar<strong>in</strong>g a variable or method. This is evenmore important if you’re provid<strong>in</strong>g a type or method which other people will call withoutaccess to your code. Generics allow library providers to prevent their users from compil<strong>in</strong>gcode that calls the library with bad arguments. Previously we’ve had to rely on manuallywritten documentation—which is often <strong>in</strong>complete or <strong>in</strong>accurate, and is rarely read anyway.Armed with the extra <strong>in</strong>formation, everyone can work more productively: the compileris able to do more check<strong>in</strong>g; the IDE is able to present IntelliSense options basedLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!