Free mag vol1 | Page 456

CHAPTER 10  DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS ***** Fun with Lambdas ***** Here are your even numbers: 20 4 8 44 Here are your even numbers: 20 4 8 44 value of i is value of i is value of i is value of i is value of i is value of i is Here are your 20 4 currently: 20 currently: 1 currently: 4 currently: 8 currently: 9 currently: 44 even numbers: 8 44  Source Code The SimpleLambdaExpressions project can be found under the Chapter 10 subdirectory. Lambda Expressions with Multiple (or Zero) Parameters The lambda expressions you have seen here processed a single parameter. This is not a requirement, however, as a lambda expression may process multiple arguments (or none). To illustrate the first scenario, create a Console Application named LambdaExpressionsMultipleParams. Next, assume the following incarnation of the SimpleMath type: public class SimpleMath { public delegate void MathMessage(string msg, int result); private MathMessage mmDelegate; public void SetMathHandler(MathMessage target) {mmDelegate = target; } } public void Add(int x, int y) { if (mmDelegate != null) mmDelegate.Invoke("Adding has completed!", x + y); } Notice that the MathMessage delegate is expecting two parameters. To represent them as a lambda expression, our Main() method might be written as follows: static void Main(string[] args) { // Register with delegate as a lambda expression. 395