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.

Inl<strong>in</strong>e delegate actions with anonymous methods147usual “braces on a l<strong>in</strong>e on their own” rule for anonymous methods (as I do for trivialproperties) but still allow a decent amount of whitespace. I’d usually write the last l<strong>in</strong>eof list<strong>in</strong>g 5.6 as someth<strong>in</strong>g likex.ForEach(delegate(<strong>in</strong>t n){ Console.WriteL<strong>in</strong>e(Math.Sqrt(n)); });The parentheses and braces are now less confus<strong>in</strong>g, and the “what it does” part standsout appropriately. Of course, how you space out your code is entirely your own bus<strong>in</strong>ess,but I encourage you to actively th<strong>in</strong>k about where you want to strike the balance,and talk about it with your teammates to try to achieve some consistency. Consistencydoesn’t always lead to the most readable code, however—sometimes keep<strong>in</strong>g everyth<strong>in</strong>gon one l<strong>in</strong>e is the most straightforward format.You should also consider how much code it makes sense to <strong>in</strong>clude <strong>in</strong> anonymousmethods. The first two examples <strong>in</strong> list<strong>in</strong>g 5.5 are reasonable , but pr<strong>in</strong>tMean is probablydo<strong>in</strong>g enough work to make it worth hav<strong>in</strong>g as a separate method. Aga<strong>in</strong>, it’s a balanc<strong>in</strong>gact.So far the only <strong>in</strong>teraction we’ve had with the call<strong>in</strong>g code is through parameters.What about return values?5.4.2 Return<strong>in</strong>g values from anonymous methodsThe Action delegate has a void return type, so we haven’t had to return anyth<strong>in</strong>gfrom our anonymous methods. To demonstrate how we can do so when we need to,we’ll use the new Predicate delegate type. We saw this briefly <strong>in</strong> chapter 3, buthere’s its signature just as a rem<strong>in</strong>der:public delegate bool Predicate(T obj)List<strong>in</strong>g 5.7 shows an anonymous method creat<strong>in</strong>g an <strong>in</strong>stance of Predicate toreturn whether the argument passed <strong>in</strong> is odd or even. Predicates are usually used <strong>in</strong>filter<strong>in</strong>g and match<strong>in</strong>g—you could use the code <strong>in</strong> list<strong>in</strong>g 5.7 to filter a list to one conta<strong>in</strong><strong>in</strong>gjust the even elements, for <strong>in</strong>stance.List<strong>in</strong>g 5.7Return<strong>in</strong>g a value from an anonymous methodPredicate isEven = delegate(<strong>in</strong>t x){ return x%2 == 0; };Console.WriteL<strong>in</strong>e(isEven(1));Console.WriteL<strong>in</strong>e(isEven(4));The new syntax is almost certa<strong>in</strong>ly what you’d have expected—we just return theappropriate value as if the anonymous method were a normal method. You may haveexpected to see a return type declared near the parameter type, but there’s no need.The compiler just checks that all the possible return values are compatible with thedeclared return type of the delegate type it’s try<strong>in</strong>g to convert the anonymousmethod <strong>in</strong>to.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!