Using C# Delegates

 

Delegates is a way to support addition of multiple features (eg: methods) to a framework without the need to recompile the core class that uses these methods. Delegate is a reference to a function/s and is itself an object (derived from MulticastDelegate) that knows how to call a method or group of methods. The instance of a Delegate can hold pointer to signature of a function or group of functions.

Delegates are used when an eventing design pattern is used OR when the caller does not need to access other methods or properties on the object implementing the method/s. If this is not the case, we use interfaces to achieve the same purpose.

Example

Methods defined in –

public class BloodThinners   {

 public void ApplyAntiCoagulant(Blood blood)       {

Console.WriteLine(“Applying AntiCoagulant”);

}

public void ApplyCirculation(Blood blood)       {

 Console.WriteLine(“Applying Circulation”);

}

public void NaturalThin(Blood blood)       {

Console.WriteLine(“Applying Natural thining..”);

}

}

Using custom Delegate –

public class BloodProcessor

{

public delegate void BloodThinnerHandler(Blood blood);

public void Process(string path, BloodThinnerHandler thinnerHandler)

{

var blood = Blood.Load(path);

var thinners = new BloodThinners();

thinnerHandler(blood);

blood.Save();

}

}

We define the custom delegate BloodThinnerHandler to handle the groups of methods that return void and that passed in object of type Blood (eg: the group of methods in the Class BloodThinners).

Using Generic Delegates –

We can use generic delegates – Action<> and Func<> to reduce the code. Action<> can be used to represent method that takes 16 param and returns nothing Func<> can be used to represent method that takes 16 params and returns one param.

public class BloodProcessor    {

//public delegate void BloodThinnerHandler(Blood blood);

public void Process(string path, Action<Blood> thinnerHandler)        {

var blood = Blood.Load(path);

var thinners = new BloodThinners();

thinnerHandler(blood);

blood.Save();

 }

}

Above uses delegate thinnerHandler of type generic-delegate (Action) that takes one argument.

Used as

class Program    {

static void Main(string[] args)        {

string path = “path”;

 var bloodProcessor = new BloodProcessor();

var BloodThinners = new BloodThinners();

//BloodProcessor.BloodThinnerHandler thinnerHandler = BloodThinners.ApplyAntiCoagulant;

Action<Blood> thinnerHandler = BloodThinners.ApplyAntiCoagulant;

 thinnerHandler += BloodThinners.ApplyCirculation;

thinnerHandler += BloodThinners.NaturalThin;

thinnerHandler += RemoveFat;

bloodProcessor.Process(path,thinnerHandler);

}

public static void RemoveFat(Blood blood)        {

Console.WriteLine(“Remove Blood Fat”);

}

}