Delegate with a simple example


Delegates in .NET. Explanation of Delegates in .NET with examples. Uses of Delegates in .NET. Tutorials on Delegates.

Introduction
This article is an attempt to explain a delegate with a simple example. Delegates are similar to function pointers in C++. For simple understanding delegates can be defiend as methods that are used to call other method. Only condition to call

another method from a delegate is that the signature of the calling methods and delegates should match.

Guidelines to be followed:

Delegates should have the same signature as the methods to be called by them.
i.e. Say that we are calling an addNumbers method which returns an integer by taking (int, int) as input. Then our

delegate also must have been declared with the same signature.
Delegate Declaration:
public delegate int simpleDelegate (int a, int b);

Method:
public int addNumber(int a, int b)


Steps to create and call a delegate:

1. Create a delegate. A delegate can be created with the delegate keyword, followed by it's return type, then name of the

delegate with the input patameters.

public delegate int simpleDelegate (int a, int b);

2. Define the methods which are having similar signature as the delegate.

public int mulNumber(int a, int b)
public int addNumber(int a, int b)

3. Now we are all set to use the delegates. Just instatiate the class and call the methods using the delegates.


Code Snippet
Just run the c# code and debug through it to see how it works.

********************************************************************************************************************


using System;

class clsDelegate
{
public delegate int simpleDelegate (int a, int b);
public int addNumber(int a, int b)
{
return (a+b);
}

public int mulNumber(int a, int b)
{
return (a*b);
}

static void Main(string[] args)
{
clsDelegate clsDlg = new clsDelegate();
simpleDelegate addDelegate = new simpleDelegate(clsDlg.addNumber);
simpleDelegate mulDelegate = new simpleDelegate(clsDlg.mulNumber);
int addAns = addDelegate(10,12);
int mulAns = mulDelegate(10,10);
Console.WriteLine("Result by calling the addNum method using a delegate: {0}",addAns);
Console.WriteLine("Result by calling the mulNum method using a delegate: {0}",mulAns);
Console.Read();
}
}


********************************************************************************************************************

Conclusion:
This example explains us about how a simple delegate works. My next article on delegates will follow in days to come.

Happy Delegating,
S Suresh

9845568305


Comments

Author: Vimal14 Oct 2004 Member Level: Bronze   Points : 0

The simple example regarding delagates was good.
Could you please provide some info about the uses of delegates.

Author: santosh narayan poojari25 Nov 2004 Member Level: Silver   Points : 0

Can u provide any instances when we should make use of delegation.
Thanks for imparting ur knowledge on delegation.

Author: Ashok s p30 Nov 2004 Member Level: Silver   Points : 0

hi,
U have explained about delegates nicely.
but can u explain more abt delegates.

Author: charmis12 Dec 2004 Member Level: Gold   Points : 0

This article is very simple to understand. Would you please tell where it is using and the importance.

Author: vikas singh05 May 2005 Member Level: Gold   Points : 0

Its simple and provide basics of using the delegates.

Author: Pratap Routray29 Dec 2005 Member Level: Bronze   Points : 0

I had some problem on understanding Delegate.After this atricle, I am felling comfortable in Delegation.

This is good for beginners.

Author: PalaniKumar.A20 Feb 2012 Member Level: Gold   Points : 4

Delegates is a type which hold the reference of a method in an object.
Mutlicast delegate is a type which hold the reference of multiple methods in an object. In order to work with Mutlicast delegates, the method should return void, otherwise run-time error will throw.

Sample Code:

public partial class _Default : System.Web.UI.Page
{
delegate int CalculateDelegate(int a, int b);
delegate void MutliCastDelegate(int a, int b);
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Delegates: ");
CalculateDelegate cDelegate = new CalculateDelegate(Add);
// call delegate
Response.Write(cDelegate(6, 9000).ToString());

Response.Write("Mutlicast Delegates: ");
MutliCastDelegate multi = new MutliCastDelegate(AddM);
multi += new MutliCastDelegate(Subtract);
// call multicast delegate
multi(50, 98999);

}
public void AddM(int a, int b)
{
Response.Write("Method 1: " + (a + b));
}
public void Subtract(int a, int b)
{
Response.Write("Method 2: " + (a - b));
}
private int Add(int a, int b)
{
return a + b;
}
}

Guest Author: dinesh23 Apr 2012

very simple example.... i can understand easily but may you explain about multicasting delegates...