CHAPTER 10 DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS
// Speed up (this will generate the events).
Console.WriteLine("\n***** Speeding up *****");
for (int i = 0; i < 6; i++)
c1.Accelerate(20);
}
Console.ReadLine();
Hopefully, at this point you can see the overall role of lambda expressions and understand how they
provide a “functional manner” to work with anonymous methods and delegate types. Although the
lambda operator (=>) might take a bit to get used to, always remember a lambda expression can be
broken down to the following simple equation:
ArgumentsToProcess => StatementsToProcessThem
It is worth pointing out that the LINQ programming model also makes substantial use of lambda
expressions to help simplify your coding efforts. You will examine LINQ beginning in Chapter 12.
Source Code The CarEventsWithLambdas project can be found under the Chapter 10 subdirectory.
Summary
In this chapter, you have examined a number of ways in which multiple objects can partake in a
bidirectional conversation. First, you looked at the C# delegate keyword, which is used to indirectly
construct a class derived from System.MulticastDelegate. As you saw, a delegate object maintains a list
of methods to call when told to do so. These invocations may be made synchronously (using the
Invoke() method) or asynchronously (via the BeginInvoke() and EndInvoke() methods). Again, the
asynchronous nature of .NET delegate types will be examined in Chapter 19.
You then examined the C# event keyword, which, when used in conjunction with a delegate type,
can simplify the process of sending your event notifications to waiting callers. As shown via the resulting
CIL, the .NET event model maps to hidden calls on the System.Delegate/System.MulticastDelegate
types. In this light, the C# event keyword is purely optional in that it simply saves you some typing time.
This chapter also explored a C# language feature termed anonymous methods. Using this syntactic
construct, you are able to directly associate a block of code statements to a given event. As you have
seen, anonymous methods are free to ignore the parameters sent by the event and have access to the
“outer variables” of the defining method. Yo u also examined a simplified way to register events using
method group conversion.
Finally, we wrapped things up by looking at the C# lambda operator, =>. As shown, this syntax is a
great shorthand notation for authoring anonymous methods, where a stack of arguments can be passed
into a group of statements for processing. Any method in the .NET platform that takes a delegate object
as an argument can be substituted with a related lambda expression, which will typically simplify your
code base quite a bit.
397