Delegates seems very daunting topic to newbies. So here I will explain you the formal definition along with example that represents what exactly the definition mean.

Formal Definition

Delegates enable to encapsulate reference of a method inside a delegate object, which then can be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Generalized Definition

A delegate in c# is similar to a function pointer in C or C++ .But c# delegates are object-oriented,type –safe and secure.

Basic English meaning of delegate : An appointed representative in some legislative bodies.

It means that delegates are the persons acting as mediators between the two parties or units etc. For example in News we come across the sentence like “the Indian delegate visited US for so and so issue “etc. So in the same way delegates in the programming has the same intuition. Delegates are used for communication between the two classes or objects etc like callbacks in functions.

Delegate Declaration

A delegate declaration defines a type that encapsulates a method with a particular set of arguments and a return type.

Static Methods: For static methods, a delegate object encapsulates the method to be called.

Instance Methods: For instance methods, a delegate object ecapsulate both an instance and a method on the instance.

Highlighting feature of delegates

A delegate doesn’t know or care about the class of the object that it references. Any object will do; all that matters is that the method’s arguments types and return types match the delegate’s. This makes delegates perfectly suited for ‘anonymous’ invocation.

Note

Delegates run under the caller’s security

Permissions, not the declarer’s permissions.

Lets understand delegates with an example.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Consoledelegates
{
delegate bool IsPromotable(Employee empl);
//delegate declaration
class Employee
{
public int id { get; set; }
public String Name { get; set; }
public int Experience { get; set; }
public int Salary { get; set; }
public static void PromoteEmployee(List empoloyeelist, IsPromotable IsEligible)
{
foreach (Employee emp in empoloyeelist)
{
if(IsEligible(emp))
//Calling the referenced method.
Console.WriteLine(emp.Name+"Promoted");

}
}
}
public class Program
{
public static void Main(string[] args)
{
List emplist = new List();
emplist.Add(new Employee() {id=101,Name="Sheeza",Experience=5,Salary=20000 });
emplist.Add(new Employee() { id = 101, Name = "Adn", Experience = 2, Salary = 10000 });
emplist.Add(new Employee() { id = 101, Name = "Raida", Experience = 3, Salary = 15000 });
emplist.Add(new Employee() { id = 101, Name = "Huda", Experience = 7, Salary = 30000 });
IsPromotable ispromoobj = new IsPromotable(Promote);
/*delegate instantiation.As per the formal definition, here ispromoobj is the delegate object which encapsulates the
reference of the Promote method.*/
Employee.PromoteEmployee(emplist, ispromoobj);
/*As per the formal definition,here the delegate object ,ispromoobj is passed to code which calls the refereced method.*/
Console.ReadLine();
}
static bool Promote(Employee emp)
{
if (emp.Experience >= 5)
return true;
else
return false;
}
}
}

Hope You understand the concept.Happy coding bye bye!!

 

Advertisements