CandidateAction);
factory("test");
Remember that Snippy3 will be generating all of this code within a class called Snippet
which the nested type derives from. Under C# 1, listing 5.4 would print Snippet.
CandidateAction because the method taking an object parameter wasn??™t compatible
with SampleDelegate. Under C# 2, however, it is compatible and is the method chosen
due to being declared in a more derived type??”so the result is that Derived.
CandidateAction is printed. Fortunately, the C# 2 compiler knows that this is a breaking
change and issues an appropriate warning.
Enough doom and gloom about potential breakage, however. We??™ve still got to see
the most important new feature regarding delegates: anonymous methods. They??™re a
bit more complicated than the topics we??™ve covered so far, but they??™re also very powerful
??”and a large step toward C# 3.
5.4 Inline delegate actions with anonymous methods
Have you ever been writing C# 1 and had to implement a delegate with a particular
signature, even though you??™ve already got a method that does what you want but
doesn??™t happen to have quite the right parameters? Have you ever had to implement a
delegate that only needs to do one teeny, tiny thing, and yet you need a whole extra
method? Have you ever been frustrated at having to navigate away from an important
bit of code in order to see what the delegate you??™re using does, only to find that the
method used is only two lines long? This kind of thing happened to me quite regularly
with C# 1.
Pages:
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298