CHAPTER 10 DELEGATES, EVENTS, AND LAMBDA EXPRESSIONS
Figure 10-3. Delegate target format IntelliSense
When you hit the Tab key again, you will be provided with stub code in the correct format of the
delegate target (note that this method has been declared static due to the fact that the event was
registered within a static method):
static void newCar_AboutToBlow(string msg)
{
// Delete the following line and add your code!
throw new NotImplementedException();
}
IntelliSense is available to all .NET events in the base class libraries. This IDE feature is a massive
time saver, given that it saves you from having to search the .NET help system to figure out both the
correct delegate to use with a particular event and the format of the delegate target method.
Source Code The CarEvents project is located under the Chapter 10 subdirectory.
Creating Custom Event Arguments
Truth be told, there is one final enhancement we could make to the current iteration of the Car class that
mirrors Microsoft’s recommended event pattern. As you begin to explore the events sent by a given type
in the base class libraries, you will find that the first parameter of the underlying delegate is a
System.Object, while the second parameter is a descendant of System.EventArgs.
The System.Object argument represents a reference to the object that sent the event (such as the
Car), while the second parameter represents information regarding the event at hand. The
System.EventArgs base class represents an event that is not sending any custom information:
public class EventArgs
{
public static readonly EventArgs Empty;
public EventArgs();
}
384