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
|
| Author: Vimal 14 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 poojari 25 Nov 2004 | Member Level: Gold Points : 0 |
Can u provide any instances when we should make use of delegation. Thanks for imparting ur knowledge on delegation.
|
| Author: Ashok s p 30 Nov 2004 | Member Level: Silver Points : 0 |
hi, U have explained about delegates nicely. but can u explain more abt delegates.
|
| Author: charmis p varghese 12 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 singh 05 May 2005 | Member Level: Gold Points : 0 |
Its simple and provide basics of using the delegates.
|
| Author: Pratap Routray 29 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.
|