This is a series of articles about Multi-threading.
- Multi-Threading (1), Concept: What, Why
- Multi-Threading (2), Implementation Overview
- Multi-Threading (3), async, await in C#
- Multi-Threading (4), delegate based Asynchronous Programming Model
- Multi-Threading (4-1), delegate and MultiThreading --- this article
- Multi-Threading (5), Summary
Introduction
The delegate play important role in the APM (Asynchorous Programming Model). Let us discuss delegate in details and its evolution from C# 1.0 to C# 3.0.
This article content is one of my technical note from a long time ago about delegates. It is related to multi-threading, I put the content into this article.
Delegate Definition and Features
Delegate definitions from different sources:
- A delegate (mine): a reference type that define a method signature so delegate instance can hold and invoke a method or a list of methods that match its signature.
- A delegate (Inside C#, P481) encapsulates methods (either static or instance) with a specific signature.
- A delegate (MSDN, event) is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature and can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback.
- delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value, (MSDN, old version)
Difference between delegates and classes:
- classes contain data,
- delegates just contain the details of a method.
Purpose of using delegate: Event handling, including both
- synchronous event handling.
- asynchronous event handling (such as: callback functionality, Starting a new thread).
Multicast Delegates
There are four ways to create a single aggregate delegate from multiple delegates:
- Part 1: using the + operator
- Part 2: using the += operator
- Part 3: Attempt To Call Combine Overload1
- Part 4: Attempt To Call Combine Overload2 (with array)
Using event is the best way where you do not need to pass the callback function with the call function and still make the callback function typesafe.
Delegate Definition Evolution from C# 1.0 to C# 3.0
Delegate definitions from different sources:
0. Without a delegate, the callback function by passing an instance from calling class to called class, and then from the called class to call back to the calling class. It works and is easy to understand.














Join the conversation! Your thoughts help the community grow.