C# Delegate - A Silent Hero Behind Modern Programming

Delegate plays an important role in today's programming model where it is extensively used in Parallel Programming, Asynchronous Programming, PLINQ, Events & Callback methods, Func, Action, Predicates and so on.

Introduction

Modern development tools & languages are full with easy use of tedious functionalities like threading, asynchronous programming, and so on. A developer just need to use ready-made mechanism (APIs/Functions) and rest is done by language or .net system library.

Background

Its looks like programming is now so simplified that anyone can write higher level code like multi-threading & asynchronous. It sound cool, but to write an efficient & optimized code, a programmer must have a good understanding on fundamental items like-
  • In terms of Hardware - CPU architecture, memory model, IO operations and OS management on resources
  • In terms of Programming – Types, Delegate, LINQ and so on.
.NET Types (in C# context)

Let’s understand the “type” in the following simple points-
  • Almost everything in .NET is object (except interface type, pointer type, open parameter type, type & args)
  • Most of types are derived from System.Object
  • There are some types which are not derived from anything.
Delegate - a true legend

Let me define to delegate the following points-

  • An object (variable), used to hold the address of function.

  • Same type of delegate objects can be composed & discomposed using the "+" and "-" operator respectively. This is called Multi-casting.
If we further drill down on above definition, we can conclude that,
  • An Object: It means delegate is of reference type (derived from System.Delegate class).

  • hold the address: (a) It is like pointers to function. (b) we can also conclude here, that this address can be changed to any function (which is of same signature).
Usage of Delegate

  1. Basic Usage: Callback methods & events implementations
  2. Advance Usage: Func, Action, Predicates and so on.
1. Basic Usage

Suppose, we want to decide which method to call on the basis of some logic which is depend upon run-time scenario or user input. Here, at compile time we are not sure about which method to call but that particular method will decide later (at run-time).
 
Like other objects, a delegate object also follow same using approach i.e. DeclareDefine and Use. Its declaration is more about the signature of function. Once it is declared, it is assigned/initialized/defined. To reach the stored address of function (it will execute/fire that function) a handy approach, Invoke() can be used if we decide to not let implicit call.

Syntax
  1. delegate <return type of targeting function> DelegateName ([parameters of targeting function]); 
Example
  1. //a delegate to hold the address of any function which takes only one parameter as string and returns int type    
  2. delegate int MyDelegateName (string name); 
We may also apply Access-modifiers on delegate, example-
  1. public delegate void MyDelegateName (string name);   
2. Advance Usage

The following are the most famous flavor of delegate- 
2.1 Func : a delegate which returns a value
2.2 Action<T> : a delegate which don't returns any value
2.3 Predicates :  a delegate which return a value of type Boolean
Summary

In modern development practices, programmers need to aware about the core components like delegate. Delegate has wider scope of usage with various flavors like multi-casting, Func, Action, Predicate and so on. Most of efficient APIs like TPL, PLINQ etc are written using the true power of delegate.

Up Next
    Ebook Download
    View all
    Learn
    View all