Multi-Threading (4-1), delegate and MultiThreading

This is a series of articles about Multi-threading.

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.

The short coming for the calling back company:

  1. it is not type safe;
  2. the called class or the publisher has to have information from the calling class or subscriber, at least the class name where the method is defined for calling back.

We could say, delegate is a typesafe calling back function.

1. C# 1.0,

By using delegate the call back is more elegant, which makes the callback method typesafe.

How to define a delegate:

1). In the called class or the server or the publisher (P482, C# inside; P477 C# 4.0):

a. define the delegate;

b. define a method that takes the delegate as one of its parameters

2). In the calling class or client or the subscriber (P483, C# inside; P478, C# 4.0):

a. Define a method that has the same signature as delegate

or:

b. Instantiate the delegate, passing it the name of the method, and passing this delegate to the called class.

2. C# 2.0,

Comparing to the last step 2). b.) in C# 1.0:

You do not need to pass the delegate to the called class, just pass the method name with the same signature as the delegate, and you do not need to use new to instantiate the delegate in the calling class.

Furthermore, by introducing the anonymous methods, you can define the delegate instance in the calling class by inline code without a method declaration in the calling class (replace: 2). a-b). (P481, C# 4.0)

You can even call the delegate parameter directly without declaring the method. (P482, C# 4.0)

3. C# 3.0,

Lambda expressions are introduced in C# 3.0 as a more succinct syntax of anonymous functions than anonymous methods, where anonymous function is a general term that includes both lambda expressions and anonymous method, providing a reduced syntax for anonymous methods, a syntax that does not include the delegate keyword and adds the lambda operator, =>. You could either use statement lambda or expression lambda (inline) (P486, C# 4.0)

Statement Lambda (without delegate key word):

Furthermore, statement Lambdas do not need parameter types as long as the compiler can infer the types or can implicitly convert them to the requisite expected types:

Expression Lambda (without statement block):

The following example demonstrates the evolution of delegate creation from C# 1.0 to C# 3.0:

4. C# 3.5

System-Defined Delegates: Action<> and Func<>

.NET 3.5 (C# 3.0) included a series of generic delegates with the names “Action” and “Func.”

  • System.Func represents delegates that had return types while
  • System.Action corresponds when no return type occurs.


Similar Articles