Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Introduction to delegates and events in C#

  • 2022-03-31
Feedback

In this article

Delegates provide alate binding mechanism in .NET. Late Bindingmeans that you create an algorithm where the caller also suppliesat least one method that implements part of the algorithm.

For example, consider sorting a list of stars in an astronomy application.You may choose to sort those stars by their distance from the earth, or themagnitude of the star, or their perceived brightness.

In all those cases, the Sort() method does essentially the same thing:arranges the items in the list based on some comparison. The code thatcompares two stars is different for each of the sort orderings.

These kinds of solutions have been used in software for half a century.The C# language delegate concept provides first class language support,and type safety around the concept.

As you'll see later in this series, the C# code you write for algorithmslike this is type safe. The compiler ensures that the types match for arguments and return types.

Function pointers support similar scenarios, where you need more control over the calling convention. The code associated with a delegate is invoked using a virtual method added to a delegate type. Using function pointers, you can specify different conventions.

Language Design Goals for Delegates

The language designers enumerated several goals for the feature thateventually became delegates.

The team wanted a common language construct that could be used forany late binding algorithms. Delegates enable developers to learn oneconcept, and use that same concept across many different softwareproblems.

Second, the team wanted to support both single and multicast methodcalls. (Multicast delegates are delegates that chain together multiple method calls.You'll see exampleslater in this series.)

The team wanted delegates to support the same type safety that developersexpect from all C# constructs.

Finally, the team recognized an event pattern is one specific patternwhere delegates, or any late binding algorithm, is useful. The teamwanted to ensure the code for delegates could provide the basis forthe .NET event pattern.

The result of all that work was the delegate and event support in C# and.NET.

The remaining articles in this series will cover languagefeatures, library support, and common idioms usedwhen you work with delegates and events.You'll learn about:

  • Thedelegate keyword and what code it generates.
  • The features in theSystem.Delegate class, and how those features are used.
  • How to create type-safe delegates.
  • How to create methods that can be invoked through delegates.
  • How to work with delegates and events by using lambda expressions.
  • How delegates become one of the building blocks for LINQ.
  • How delegates are the basis for the .NET event pattern, and how they're different.

Let's get started.

Next

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo