CHAPTER 10 DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS
// Register event handlers.
c1.AboutToBlow += CarIsAlmostDoomed;
c1.AboutToBlow += CarAboutToBlow;
c1.Exploded += CarExploded;
Console.WriteLine("***** Speeding up *****");
for (int i = 0; i < 6; i++)
c1.Accelerate(20);
c1.Exploded -= CarExploded;
Console.WriteLine("\n***** Speeding up *****");
for (int i = 0; i < 6; i++)
c1.Accelerate(20);
Console.ReadLine();
}
Simplifying Event Registration Using Visual Studio
Visual Studio offers assistance with the process of registering event handlers. When you apply the +=
syntax during event registration, you will find an IntelliSense window displayed, inviting you to hit the
Tab key to autocomplete the associated delegate instance (see Figure 10-2), which is captured using
method group conversion syntax.
Figure 10-2. Delegate selection IntelliSense
After you hit the Tab key, you are invited to enter the name of the event handler to be generated (or
simply accept the default name), as shown in Figure 10-3.
383