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.

138 CHAPTER 5 Fast-tracked delegates<strong>C#</strong> 2 is a sort of stepp<strong>in</strong>g stone <strong>in</strong> terms of delegates. Its new features pave the wayfor the even more dramatic changes of <strong>C#</strong> 3, keep<strong>in</strong>g developers reasonably comfortablewhile still provid<strong>in</strong>g useful benefits. The extent to which this was a f<strong>in</strong>ely balancedact as opposed to <strong>in</strong>tuition and a follow<strong>in</strong>g w<strong>in</strong>d is likely to stay unknown, butwe can certa<strong>in</strong>ly reap the benefits.Delegates play a more prom<strong>in</strong>ent part <strong>in</strong> .NET 2.0 than <strong>in</strong> earlier versions,although they’re not as common as they are <strong>in</strong> .NET 3.5. In chapter 3 we saw how theycan be used to convert from a list of one type to a list of another type, and way back <strong>in</strong>chapter 1 we sorted a list of products us<strong>in</strong>g the Comparison delegate <strong>in</strong>stead of theIComparer <strong>in</strong>terface. Although the framework and <strong>C#</strong> keep a respectful distance fromeach other where possible, I believe that the language and platform drove each otherhere: the <strong>in</strong>clusion of more delegate-based API calls supported the improved syntaxavailable <strong>in</strong> <strong>C#</strong> 2, and vice versa.In this chapter we’ll see how <strong>C#</strong> 2 makes two small changes that make life easierwhen creat<strong>in</strong>g delegate <strong>in</strong>stances from normal methods, and then we’ll look at thebiggest change: anonymous methods, which allow you to specify a delegate <strong>in</strong>stance’saction <strong>in</strong>l<strong>in</strong>e at the po<strong>in</strong>t of its creation. The largest section of the chapter is devotedto the most complicated part of anonymous methods, captured variables, which providedelegate <strong>in</strong>stances with a richer environment to play <strong>in</strong>. We’ll cover the topic <strong>in</strong>significant detail due to its importance and complexity.First, though, let’s rem<strong>in</strong>d ourselves of the pa<strong>in</strong> po<strong>in</strong>ts of <strong>C#</strong> 1’s delegate facilities.5.1 Say<strong>in</strong>g goodbye to awkward delegate syntaxThe syntax for delegates <strong>in</strong> <strong>C#</strong> 1 doesn’t sound too bad—the language already has syntacticsugar around Delegate.Comb<strong>in</strong>e, Delegate.Remove, and the <strong>in</strong>vocation of delegate<strong>in</strong>stances. It makes sense to specify the delegate type when creat<strong>in</strong>g a delegate<strong>in</strong>stance—it’s the same syntax used to create <strong>in</strong>stances of other types, after all.This is all true, but for some reason it also sucks. It’s hard to say exactly why the delegatecreation expressions of <strong>C#</strong> 1 raise hackles, but they do—at least for me. Whenhook<strong>in</strong>g up a bunch of event handlers, it just looks ugly to have to write “newEventHandler” (or whatever is required) all over the place, when the event itself hasspecified which delegate type it will use. Beauty is <strong>in</strong> the eye of the beholder, of course,and you could argue that there’s less call for guesswork when read<strong>in</strong>g event handler wir<strong>in</strong>gcode <strong>in</strong> the <strong>C#</strong> 1 style, but the extra text just gets <strong>in</strong> the way and distracts from theimportant part of the code: which method you want to handle the event.Life becomes a bit more black and white when you consider covariance and contravarianceas applied to delegates. Suppose you’ve got an event handl<strong>in</strong>g methodthat saves the current document, or just logs that it’s been called, or any number ofother actions that may well not need to know details of the event. The event itselfshouldn’t m<strong>in</strong>d that your method is capable of work<strong>in</strong>g with only the <strong>in</strong>formation providedby the EventHandler signature, even though it is declared to pass <strong>in</strong> mouseevent details. Unfortunately, <strong>in</strong> <strong>C#</strong> 1 you have to have a different method for each differentevent handler signature.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!