Raising and handling events
Methods are often described as actions that an object can do. For example, a List
class can add an item to itself or clear itself.
Events are often described as actions that happen to an object. For example, in a user interface, Button
has a Click
event, click being something that happens to a button.
Another way of thinking of events is that they provide a way of exchanging messages between two objects.
Calling methods using delegates
You have already seen the most common way to call or execute a method: use the dot syntax to access the method using its name. For example, Console.WriteLine
tells the Console
type to write out the message to the console window or Terminal.
The other way to call or execute a method is to use a delegate. If you have used languages that support function pointers, then think of a delegate as being a type-safe method pointer. In other words, a delegate is the memory address of a method that matches the same signature as the delegate so that...