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 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 ...

Get C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.