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.

Simple generics for everyday use65on the extra <strong>in</strong>formation (for <strong>in</strong>stance, offer<strong>in</strong>g the members of str<strong>in</strong>g as next stepswhen you access an element of a list of str<strong>in</strong>gs); callers of methods can be more certa<strong>in</strong>of correctness <strong>in</strong> terms of arguments passed <strong>in</strong> and values returned; and anyone ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>gyour code can better understand what was runn<strong>in</strong>g through your head when youorig<strong>in</strong>ally wrote it <strong>in</strong> the first place.NOTE Will generics reduce your bug count? Every description of generics I’ve read(<strong>in</strong>clud<strong>in</strong>g my own) emphasizes the importance of compile-time typecheck<strong>in</strong>g over execution-time type check<strong>in</strong>g. I’ll let you <strong>in</strong> on a secret: Ican’t remember ever fix<strong>in</strong>g a bug <strong>in</strong> released code that was directly dueto the lack of type check<strong>in</strong>g. In other words, the casts we’ve been putt<strong>in</strong>g<strong>in</strong> our <strong>C#</strong> 1 code have always worked <strong>in</strong> my experience. Those casts havebeen like warn<strong>in</strong>g signs, forc<strong>in</strong>g us to th<strong>in</strong>k about the type safety explicitlyrather than it flow<strong>in</strong>g naturally <strong>in</strong> the code we write. Although genericsmay not radically reduce the number of type safety bugs you encounter,the greater readability afforded can reduce the number of bugs acrossthe board. Code that is simple to understand is simple to get right.All of this would be enough to make generics worthwhile—but there are performanceimprovements too. First, as the compiler is able to perform more check<strong>in</strong>g, that leavesless need<strong>in</strong>g to be checked at execution time. Second, the JIT is able to treat value types<strong>in</strong> a particularly clever way that manages to elim<strong>in</strong>ate box<strong>in</strong>g and unbox<strong>in</strong>g <strong>in</strong> many situations.In some cases, this can make a huge difference to performance <strong>in</strong> terms ofboth speed and memory consumption.Many of the benefits of generics may strike you as be<strong>in</strong>g remarkably similar to thebenefits of static languages over dynamic ones: better compile-time check<strong>in</strong>g, more<strong>in</strong>formation expressed directly <strong>in</strong> the code, more IDE support, better performance.The reason for this is fairly simple: when you’re us<strong>in</strong>g a general API (for example,ArrayList) that can’t differentiate between the different types, you effectively are <strong>in</strong> adynamic situation <strong>in</strong> terms of access to that API. The reverse isn’t generally true, by theway—there are plenty of benefits available from dynamic languages <strong>in</strong> many situations,but they rarely apply to the choice between generic/nongeneric APIs. When youcan reasonably use generics, the decision to do so is usually a no-bra<strong>in</strong>er.So, those are the goodies await<strong>in</strong>g us <strong>in</strong> <strong>C#</strong> 2—now it’s time to actually start us<strong>in</strong>ggenerics.3.2 Simple generics for everyday useThe topic of generics has a lot of dark corners if you want to know everyth<strong>in</strong>g about it.The <strong>C#</strong> 2 language specification goes <strong>in</strong>to a great deal of detail <strong>in</strong> order to make surethat the behavior is specified <strong>in</strong> pretty much every conceivable case. However, wedon’t need to understand most of those corner cases <strong>in</strong> order to be productive. (Thesame is true <strong>in</strong> other areas, <strong>in</strong> fact. For example, you don’t need to know all the exactrules about def<strong>in</strong>itely assigned variables—you just fix the code appropriately when thecompiler compla<strong>in</strong>s.)Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!