Click here to Skip to main content
13,902,979 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 Jan 2015
Licenced CPOL

Delegates, Anonymous Methods and Lambda Expressions

, 1 Jan 2015
Rate this:
Please Sign up or sign in to vote.
Brief description about Anonymous Methods and Lambda Expressions using Delegates.

What is Delegate ?

Delegates are a reference type variable that hold a reference to any particular method, the reference can be changed at run time. So in short, Delegates are nothing itself unless and until they refer to any method. Syntax for defining a Delegate is as follows:

delegate <return type> <delgate-name><parameter list>

Rules for Defining Delegate

  1. A Delegate should have the same signature as that of a method it is referring to.
  2. An instance of Delegate can refer to only one method (i.e. if you want to refer to any other method, you should create other object of delegate for doing that).
  3. Once a delegate type has been declared, a delegate object must be created with the new keyword and be associated with a particular method. A method would be invoked into delegates without the arguments to method.

Simple Example of Using Delegate

delegate int ChangeInt (int a); // creating delegate

class Program

//our named method 
static public int DoubleIt(int x)
return x * 2;

//main method
static void Main(string[] args)
ChangeInt myDel = new ChangeInt(doubleIt);      //referring to named method using delegate.
Console.WriteLine("Output: {0}" , myDel(5));    //invoking DoubleIt method through delegate object
//Output: 10

What is Anonymous Method?

C# 2.0 introduced anonymous methods. You can think of an anonymous method as a method which have no name no return type but only have a parameters list and a method body containing statements to be executed. Anonymous methods were added primarily so that you can define delegates without having to create a named method. Note that you have to use a delegate keyword while defining anonymous method.

Following is the same example as above but this time we'll use anonymous methods instead of named method to perform the task:

delegate int ChangeInt (int a); 

class Program

static void Main(string[] args)
ChangeInt MyDel = delegate(int x){return x * 2;}; //referring to anonymous method
Console.WriteLine("Output: {0}" , MyDel(5));      //invoking anonymous method using delegate object
//Output: 10

What are Lambda Expressions?

C# 3.0 introduced Lambda Expressions. They are similar in concept to anonymous methods but are more expressive and concise. A lambda expression is an inline function that also doesn't have a return type but can return a value. You can define lambda expressions using this sign => on the left of this sign you can define parameters without declaring their types (because compiler will do it for you) and on the right side you can define your statements to be executed. Note that they are also used with the delegates.

Following is the same example as above but this I have used lambda expressions instead if anonymous method:

delegate int ChangeInt (int a); 

class Program

static void Main(string[] args)
ChangeInt MyDel = x => x * 2;                 //referring delegate to a lambda expressions
Console.WriteLine("Output: {0}" , MyDel(5));      
//Output: 10

In the above example, x is a parameter which is defined on the left of => sign and on right x * 2 is our statement to be executed. The type of variable x is inferred by the compiler. You can also define multiple parameters by using brackets like this: (a,b) => a*b;

Now take a look at the first example where we have used named methods and now look at the above example where we have used lambda expressions. Now note how we can perform the same task in minimum code and minimum time.

Now you are able to use delegates with named methods, anonymous methods and with lambda expressions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

M.Osama Shafi
Software Developer (Senior) Axact
Pakistan Pakistan
I am a senior software engineer , eager to learn new technologies , C# certified and want to gain as much experience in the field of software specially in Microsoft dot net framework

You may also be interested in...

Comments and Discussions

QuestionNeed help with delegates Pin
Member 94728023-Jan-15 6:39
memberMember 94728023-Jan-15 6:39 
AnswerRe: Need help with delegates Pin
M.Osama Shafi3-Jan-15 22:39
memberM.Osama Shafi3-Jan-15 22:39 
QuestionOne Wrong Point about delegates Pin
AshishVishwakarma3-Jan-15 6:35
professionalAshishVishwakarma3-Jan-15 6:35 
AnswerRe: One Wrong Point about delegates Pin
M.Osama Shafi3-Jan-15 22:50
memberM.Osama Shafi3-Jan-15 22:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 1 Jan 2015
Article Copyright 2015 by M.Osama Shafi
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid