Delegates , Anonymous functions and Generic Delegate -Func<> C#

Mirza Farrukh Ali
3 min readSep 10, 2017

--

Delegate:

It is a reference type variable with an object which holds the reference to a method(also called Function pointer). The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Delegate Declaration will have ‘Delegate’ keyword, Delegate Name with the return type and parameter of the method which reference it intends to hold.Assign, any method that matches this signature, to the delegate and it will be called each time delegate is called.

Instantiating Delegates : a delegate object must be created and associated with a particular method. When creating a delegate, however, the argument passed is method call, but without the arguments to the method.

For static methods, a delegate object encapsulates the method to be called. For instance methods, a delegate object encapsulates both an instance and a method on the instance.

DelegateType delegateTypevariable=new DelegateType(object.methodName)//for Non static method

DelegateType delegateTypevariable=new DelegateType(className.methodName)//for static method

Calling a Delegate:Once a delegate object is created, the delegate object is typically passed to other code that will call the delegate. A delegate object is called by using the name of the delegate object, followed by the parenthesized arguments to be passed to the delegate

delegateTypeVariable(param1,param2);

Stage1:Normal Delegate

Class Main{

Delegate double AreaOfCircleDel(int r);//delegate declaration

Public static void main(){

Static AreaOfCircleDel areaOfCircle= AreaOFCircleFunc;//instantiating delegate

Double area=areaOfCircle(10);

}

static Double AreaOFCircleFunc(int radius){//method defination

return 3.14*radius*radius

}

}

Delegates are used to pass methods as parameters to other methods:

Advantage of Delegate:Here print method below remains the same when delegate point to circle or square area or any method with matching syntax.

Class Main{

Delegate double AreaDel(int r);//delegate declaration

Public static void main(){

Static AreaDel areaOfCircle= AreaOFCircleFunc;//instantiating delegate
Static AreaDel areaOfSquare= AreaOFSquareFunc;//instantiating delegate
area(areaOfCircle) or area(areaOfSquare)//Calling a Delegate

}
static Double AreaOFCircleFunc(int radius){

return 3.14*radius*radius

}
static Double AreaOFSquareFunc(int side){

return side*side

}

}
public static void area(AreaDel delegatGetArea ){

delegatGetArea(9); //perform operation

}

Anonymous Method:

In C# 2.0,to avoid creating complete method for each delegate we use,we define anonymous method.You can associate a delegate directly to a block of code statements at the time of event registration. Such code is named anonymous method.

delegate(int x)
{
Console.WriteLine(“Anonymous Method: {0}”, x);
};

Stage 2: Delegate Anonymous method

Class main{

Delegate double AreaOfCircleDel(int r);

Public static void main()

{

AreaOfCircleDel areaOfCircle= delegate(int r){

Return 3.14*r*r;

};

Double area=areaOfCircle(10);

}

}

Lamda expressions:

In C#3.0,Anonymous Method reduced syntax with Lamda Expression.Both are useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class. you specify input parameters (if any) on the left side of the lambda operator =>, and you put the expression or statement block on the other side. It is used extensively in the construction of Expression Trees.

Examples: x=>x*3,(x,y)=>x==y and ( )=>method( ).

Stage 3: delegate Lamda expression

Class main{

delegate double AreaOfCircleDel(int r);

Public static void main(){

AreaOfCircleDel areaOfCircle= r=>3.14*r*r;

Double area=areaOfCircle(10);

}

}

Generic Delegate:

public delegate string GenericDelegate<T1,T2>(T1 a,T2 b);

GenericDelegate<int,int> genericDel=new GenericDelegate<int,int>(add)

Func Delegate:

It is a generic delegate present in System namespace which takes 0 to 16 input parameters and must return 1 out parameter.Use Func delegate without explicitly declaring a custom delegate.

It is a generic delegate present in System namespace which takes 0 to 16 input parameters and must return 1 out parameter.Use Func delegate without explicitly declaring a custom delegate.

Stage 4:Generic delegate(Func) plus lambda

Class main{

Public static void main()

{

Func<int,Double> areaOfCircle= r=>3.14*r*r;

Double area=areaOfCircle(10);

}

}

--

--