C# Delegate – a silent hero behind modern programming

This post is about the usage of Delegate and its Power (multi-casting, Func, Action, Predicate, Event & Callback methods 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. Its 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 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 in 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. Declare, Define 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: 

delegate <return type of targeting function> DelegateName ([parameters of targeting function]); 
ex. –
//a delegate to hold the address of any function which takes only one parameter as string and returns int type
delegate int MyDelegateName (string name); 

We can also apply access modifier on it, ex-
public delegate void MyDelegateName (string name);

2. Advance usage-

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