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.

146 CHAPTER 5 Fast-tracked delegatespr<strong>in</strong>tReverse("Hello world");pr<strong>in</strong>tRoot(2);pr<strong>in</strong>tMean(samples);List<strong>in</strong>g 5.5 shows a few of the different features of anonymous methods.First, the syntax of anonymous methods: use the delegate keyword, followedby the parameters (if there are any), followed by the code for theaction of the delegate <strong>in</strong>stance, <strong>in</strong> a block. The str<strong>in</strong>g reversal code Bshows that the block can conta<strong>in</strong> local variable declarations, and the “listaverag<strong>in</strong>g” code C demonstrates loop<strong>in</strong>g with<strong>in</strong> the block. Basically, anyth<strong>in</strong>gyou can do <strong>in</strong> a normal method body, you can do <strong>in</strong> an anonymousmethod. 4 Likewise, the result of an anonymous method is a delegate<strong>in</strong>stance that can be used like any other one D. Be warned that contravariancedoesn’t apply to anonymous methods: you have to specify the parameter types thatmatch the delegate type exactly.In terms of implementation, we are still creat<strong>in</strong>g a method for each delegate<strong>in</strong>stance: the compiler will generate a method with<strong>in</strong> the class and use that as theaction it uses to create the delegate <strong>in</strong>stance, just as if it were a normal method. TheCLR neither knows nor cares that an anonymous method was used. You can see theextra methods with<strong>in</strong> the compiled code us<strong>in</strong>g ildasm or Reflector. (Reflector knowshow to <strong>in</strong>terpret the IL to display anonymous methods <strong>in</strong> the method that uses them,but the extra methods are still visible.)It’s worth po<strong>in</strong>t<strong>in</strong>g out at this stage that list<strong>in</strong>g 5.5 is “exploded” compared withhow you may well see anonymous methods <strong>in</strong> real code. You’ll often see them usedas parameters to another method (rather than assigned to a variable of the delegatetype) and with very few l<strong>in</strong>e breaks—compactness is part of the reason forus<strong>in</strong>g them, after all. For example, we mentioned <strong>in</strong> chapter 3 that List has aForEach method that takes an Action as a parameter and performs that actionon each element. List<strong>in</strong>g 5.6 shows an extreme example of this, apply<strong>in</strong>g the same“square root<strong>in</strong>g” action we used <strong>in</strong> list<strong>in</strong>g 5.5, but <strong>in</strong> a compact form.Anonymousmethodsjust conta<strong>in</strong>normal codeDInvokes delegatesas normalList<strong>in</strong>g 5.6Extreme example of code compactness. Warn<strong>in</strong>g: unreadable code ahead!List x = new List();x.Add(5);x.Add(10);x.Add(15);x.Add(20);x.Add(25);x.ForEach(delegate(<strong>in</strong>t n){Console.WriteL<strong>in</strong>e(Math.Sqrt(n));});That’s pretty horrendous—especially when at first sight the last six characters appear tobe ordered almost at random. There’s a happy medium, of course. I tend to break my4One slight oddity is that if you’re writ<strong>in</strong>g an anonymous method <strong>in</strong> a value type, you can’t reference this fromwith<strong>in</strong> it. There’s no such restriction with<strong>in</strong> a reference type.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!