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.

Inl<strong>in</strong>e delegate actions with anonymous methods145no need to “pollute” the rest of your class with an extra method conta<strong>in</strong><strong>in</strong>g a smallpiece of code that is only useful <strong>in</strong> one place and doesn’t make sense elsewhere.Anonymous methods also provide some far more powerful behavior <strong>in</strong> the formof closures, but we’ll come to them <strong>in</strong> section 5.5. For the moment, let’s stick with relativelysimple stuff—as you may have noticed, a common theme <strong>in</strong> this book is thatyou can go a long way <strong>in</strong> <strong>C#</strong> 2 without deal<strong>in</strong>g with the more complex aspects of thelanguage. Not only is this good <strong>in</strong> terms of learn<strong>in</strong>g the new features gradually, butif you only use the more complicated areas when they provide a lot of benefit, yourcode will be easier to understand as well. First we’ll see examples of anonymousmethods that take parameters but don’t return any values; then we’ll explore thesyntax <strong>in</strong>volved <strong>in</strong> provid<strong>in</strong>g return values and a shortcut available when we don’tneed to use the parameters passed to us.5.4.1 Start<strong>in</strong>g simply: act<strong>in</strong>g on a parameterIn chapter 3 we saw the Action delegate type. As a rem<strong>in</strong>der, its signature is verysimple (aside from the fact that it’s generic):public delegate void Action(T obj)In other words, an Action does someth<strong>in</strong>g with an <strong>in</strong>stance of T. So anAction could reverse the str<strong>in</strong>g and pr<strong>in</strong>t it out, an Action could pr<strong>in</strong>tout the square root of the number passed to it, and an Action couldf<strong>in</strong>d the average of all the numbers given to it and pr<strong>in</strong>t that out. By complete co<strong>in</strong>cidence,these examples are all implemented us<strong>in</strong>g anonymous methods <strong>in</strong> list<strong>in</strong>g 5.5.List<strong>in</strong>g 5.5Anonymous methods used with the Action delegate typeAction pr<strong>in</strong>tReverse = delegate(str<strong>in</strong>g text){char[] chars = text.ToCharArray();Array.Reverse(chars);Console.WriteL<strong>in</strong>e(new str<strong>in</strong>g(chars));};BUses anonymousmethod to createActionAction pr<strong>in</strong>tRoot = delegate(<strong>in</strong>t number){Console.WriteL<strong>in</strong>e(Math.Sqrt(number));};Action pr<strong>in</strong>tMean = delegate(IList numbers){double total = 0;foreach (double value <strong>in</strong> numbers){total += value;}Console.WriteL<strong>in</strong>e(total/numbers.Count);};CUses loop <strong>in</strong>anonymousmethoddouble[] samples = {1.5, 2.5, 3, 4.5};Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!