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 use73}...List <strong>in</strong>tegers = new List();<strong>in</strong>tegers.Add(1);<strong>in</strong>tegers.Add(2);<strong>in</strong>tegers.Add(3);<strong>in</strong>tegers.Add(4);Converter converter = TakeSquareRoot;BCreates andpopulates listof <strong>in</strong>tegersCCreates delegate<strong>in</strong>stanceList doubles;doubles = <strong>in</strong>tegers.ConvertAll(converter);foreach (double d <strong>in</strong> doubles){Console.WriteL<strong>in</strong>e (d);}The creation and population of the list B is straightforward enough—it’s just a stronglytyped list of <strong>in</strong>tegers. C uses a feature of delegates (method group conversions), whichis new to <strong>C#</strong> 2 and which we’ll discuss <strong>in</strong> more detail <strong>in</strong> section 5.2. Although I don’t likeus<strong>in</strong>g a feature before describ<strong>in</strong>g it fully, the l<strong>in</strong>e would just have been too long to fit onthe page with the full version. It does what you expect it to, though. At D we call thegeneric method, specify<strong>in</strong>g the type argument for the method <strong>in</strong> the same way as we’veseen for generic types. We’ll see later (section 3.3.2) that you don’t always need to specifythe type argument—often the compiler can work it out itself, mak<strong>in</strong>g the code that bitmore compact. We could have omitted it this time, but I wanted to show the full syntax.Writ<strong>in</strong>g out the list that has been returned is simple, and when you run the code you’llsee it pr<strong>in</strong>t 1, 1.414..., 1.732..., and 2, as expected.So, what’s the po<strong>in</strong>t of all of this? We could have just used a foreach loop to gothrough the <strong>in</strong>tegers and pr<strong>in</strong>ted out the square root immediately, of course, but it’snot at all uncommon to want to convert a list of one type to a list of another by perform<strong>in</strong>gsome logic on it. The code to do it manually is still simple, but it’s easier to read aversion that just does it <strong>in</strong> a s<strong>in</strong>gle method call. That’s often the way with generic methods—theyoften do th<strong>in</strong>gs that previously you’d have happily done “longhand” but thatare just simpler with a method call. Before generics, there could have been a similaroperation to ConvertAll on ArrayList convert<strong>in</strong>g from object to object, but it wouldhave been a lot less satisfactory. Anonymous methods (see section 5.4) also help here—if we hadn’t wanted to <strong>in</strong>troduce an extra method, we could just have specified the conversion“<strong>in</strong>l<strong>in</strong>e.”Note that just because a method is generic doesn’t mean it has to be part of ageneric type. List<strong>in</strong>g 3.3 shows a generic method be<strong>in</strong>g declared and used with<strong>in</strong> aperfectly normal class.DCalls genericmethod toconvert listList<strong>in</strong>g 3.3Implement<strong>in</strong>g a generic method <strong>in</strong> a nongeneric typestatic List MakeList (T first, T second){List list = new List();list.Add (first);Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!