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.

74 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericslist.Add (second);return list;}...List list = MakeList ("L<strong>in</strong>e 1", "L<strong>in</strong>e 2");foreach (str<strong>in</strong>g x <strong>in</strong> list){Console.WriteL<strong>in</strong>e (x);}The MakeList generic method only needs one type parameter (T). All it does is builda list conta<strong>in</strong><strong>in</strong>g the two parameters. It’s worth not<strong>in</strong>g that we can use T as a type argumentwhen we create the List <strong>in</strong> the method, however. Just as when we were look<strong>in</strong>gat generic declarations, th<strong>in</strong>k of the implementation as (roughly speak<strong>in</strong>g) replac<strong>in</strong>g allof the places where it says T with str<strong>in</strong>g. When we call the method, we use the same syntaxwe’ve seen before. In case you were wonder<strong>in</strong>g, a generic method with<strong>in</strong> a generictype doesn’t have to use the generic type’s type parameters—although most do.All OK so far? You should now have the hang of “simple” generics. There’s a bitmore complexity to come, I’m afraid, but if you’re happy with the fundamental ideaof generics, you’ve jumped the biggest hurdle. Don’t worry if it’s still a bit hazy—particularlywhen it comes to the open/closed/unbound/constructed term<strong>in</strong>ology—butnow would be a good time to do some experimentation so you can see generics <strong>in</strong>action before we go any further.The most important types to play with are List and Dictionary.A lot of the time you can get by just by <strong>in</strong>st<strong>in</strong>ct and experimentation, but if you wantmore details of these types, you can skip ahead to sections 3.5.1 and 3.5.2. Once you’reconfident us<strong>in</strong>g these types, you should f<strong>in</strong>d that you rarely want to use ArrayList orHashtable anymore.One th<strong>in</strong>g you may f<strong>in</strong>d when you experiment is that it’s hard to only go part ofthe way. Once you make one part of an API generic, you often f<strong>in</strong>d that you need torework other code to either also be generic or to put <strong>in</strong> the casts required by the morestrongly typed method calls you have now. An alternative can be to have a stronglytyped implementation, us<strong>in</strong>g generic classes under the covers, but leav<strong>in</strong>g a weaklytyped API for the moment. As time goes on, you’ll become more confident aboutwhen it’s appropriate to use generics.3.3 Beyond the basicsWhile the relatively simple uses of generics we’ve seen can get you a long way, thereare some more features available that can help you further. We’ll start off by exam<strong>in</strong><strong>in</strong>gtype constra<strong>in</strong>ts, which allow you more control over which type arguments can bespecified. They are useful when creat<strong>in</strong>g your own generic types and methods, andyou’ll need to understand them <strong>in</strong> order to know what options are available whenus<strong>in</strong>g the framework, too.We’ll then exam<strong>in</strong>e type <strong>in</strong>ference —a handy compiler trick that means that whenyou’re us<strong>in</strong>g generic methods, you don’t always have to explicitly state the typeLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!