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.

144 CHAPTER 5 Fast-tracked delegatesList<strong>in</strong>g 5.4 Demonstration of break<strong>in</strong>g change between <strong>C#</strong> 1 and <strong>C#</strong> 2delegate void SampleDelegate(str<strong>in</strong>g x);public void CandidateAction(str<strong>in</strong>g x){Console.WriteL<strong>in</strong>e("Snippet.CandidateAction");}public class Derived : Snippet{public void CandidateAction(object o){Console.WriteL<strong>in</strong>e("Derived.CandidateAction");}}...Derived x = new Derived();SampleDelegate factory = new SampleDelegate(x.CandidateAction);factory("test");Remember that Snippy 3 will be generat<strong>in</strong>g all of this code with<strong>in</strong> a class called Snippetwhich the nested type derives from. Under <strong>C#</strong> 1, list<strong>in</strong>g 5.4 would pr<strong>in</strong>t Snippet.CandidateAction because the method tak<strong>in</strong>g an object parameter wasn’t compatiblewith SampleDelegate. Under <strong>C#</strong> 2, however, it is compatible and is the method chosendue to be<strong>in</strong>g declared <strong>in</strong> a more derived type—so the result is that Derived.CandidateAction is pr<strong>in</strong>ted. Fortunately, the <strong>C#</strong> 2 compiler knows that this is a break<strong>in</strong>gchange and issues an appropriate warn<strong>in</strong>g.Enough doom and gloom about potential breakage, however. We’ve still got to seethe most important new feature regard<strong>in</strong>g delegates: anonymous methods. They’re abit more complicated than the topics we’ve covered so far, but they’re also very powerful—anda large step toward <strong>C#</strong> 3.5.4 Inl<strong>in</strong>e delegate actions with anonymous methodsHave you ever been writ<strong>in</strong>g <strong>C#</strong> 1 and had to implement a delegate with a particularsignature, even though you’ve already got a method that does what you want butdoesn’t happen to have quite the right parameters? Have you ever had to implement adelegate that only needs to do one teeny, t<strong>in</strong>y th<strong>in</strong>g, and yet you need a whole extramethod? Have you ever been frustrated at hav<strong>in</strong>g to navigate away from an importantbit of code <strong>in</strong> order to see what the delegate you’re us<strong>in</strong>g does, only to f<strong>in</strong>d that themethod used is only two l<strong>in</strong>es long? This k<strong>in</strong>d of th<strong>in</strong>g happened to me quite regularlywith <strong>C#</strong> 1. The covariance and contravariance features we’ve just talked about cansometimes help with the first problem, but often they don’t. Anonymous methods, whichare also new <strong>in</strong> <strong>C#</strong> 2, can pretty much always help with these issues.Informally, anonymous methods allow you to specify the action for a delegate<strong>in</strong>stance <strong>in</strong>l<strong>in</strong>e as part of the delegate <strong>in</strong>stance creation expression. This means there’s3In case you skipped the first chapter, Snippy is a tool I’ve used to create short but complete code samples. Seesection 1.4.2 for more details.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!