Why use delegate in C#?
My Experience with Delegates
Purpose
- Encapsulation / Abstraction
- Security
- Callback
- Re-usability
- Type safe
- Takes method in assignment
using System;
namespace Models
{
public class Products
{
public string ProductName { get; set; }
public int ProductId { get; set; }
}
}Let’s create and interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLayer
{
public interface ICustomer<T>
{
void Process(T products);
}
}Second, inherit this interface in class
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLayer
{
public class FrequentCustomer : ICustomer<Products>
{
public void Process(Products product)
{
Console.WriteLine($"Product Count : 1");
Console.WriteLine("--Product Details--");
Console.WriteLine($"Name : {product.ProductName} Product Id : {product.ProductId}");
}
}
}Process is the method which will be called by an anonymous later
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceCallManager
{
public static class ServiceCaller
{
public static void Invoke<TService>(Action<TService> action)
{
Type typ = typeof(TService);
TService instance = (TService)Activator.CreateInstance(typ);
action(instance);
}
}
}Invoke is the method which takes Action<TService> type argument, in .Net Action delegate's return type is void.

class Program
{
static void Main(string[] args)
{
IList<Products> products = new List<Products>();
Products product1 = new Products();
product1.ProductName = "Rice";
product1.ProductId = 1;
products.Add(product1);
product1 = new Products();
product1.ProductName = "Dal";
product1.ProductId = 2;
products.Add(product1);
product1 = new Products();
product1.ProductName = "Chana";
product1.ProductId = 3;
products.Add(product1);
ServiceCaller.Invoke<FrequentCustomer>(x => x.Process(product1));
Console.WriteLine();
Console.ReadKey();
}
}
ServiceCaller.Invoke<FrequentCustomer>(x => x.Process(product1));Remember that the invoke method accepts delegate as argument and delegate encapsulates methods, so here inside Invoke method I am passing an anonymous method which will get invoked when action() is called which executes the anonymous method and calls the Process method of FrequentCustomer class.
- Breakpoint comes to ServiceCaller.Invoke<FrequentCustomer>(x => x.Process(product1));
- It goes inside Invoke method of staticServiceCaller class
- Via reflection we are creating object of ServiceT type (You can ignore this if you don’t understand reflection)
- At the last line of method action is called with parameter instance; i.e. object of ServiceT type
- After action (instance) is called breakpoint comes to ServiceCaller.Invoke<FrequentCustomer>(x => x.Process(product1)); and it starts executing the x => x.Process(product1) part only
- You will notice that x is of type FrequentCustomer

- So this part of execution calls the Process method or FrequentCustomer and is also passing local parameter in it.


Amit MohantyPosted Nov 8, 2020, 6:47 PM EST
Very helpful to the readers.
Rajanikant HawaldarPosted Nov 7, 2020, 6:36 PM EST
Informative blog