C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Delegate with a simple example


Posted Date: 07 Oct 2004    Resource Type: Articles    Category: .NET Framework
Author: S. SureshMember Level: Gold    
Rating: 1 out of 5Points: 7



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



Responses

Author: Vimal    14 Oct 2004Member 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 2004Member 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 2004Member Level: Silver   Points : 0
hi,
U have explained about delegates nicely.
but can u explain more abt delegates.


Author: charmis p varghese    12 Dec 2004Member 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 2005Member Level: Gold   Points : 0
Its simple and provide basics of using the delegates.


Author: Pratap Routray    29 Dec 2005Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Converting from Binary to Integer and Integer to Binary
Previous Resource: Typed DataSet
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use