C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Delegates


Posted Date: 27 May 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Ravindra Kumar Singh       Member Level: Gold
Rating:     Points: 10



A delegate contains a reference to a method rather than the method name itself. By using delegates, you can decide which method to call at runtime. It is also possible to call a method without actually knowing its name. Executing (or calling) a delegate will, in turn, execute the method that it references.

for example, consider two methods SumMe() and DiffMe(). SumMe() takes in two integer type parameters, and returns the sum of the numbers as another integer type. Similarly, DiffMe() takes in two integer type parameters, subtracts the second from the first and returns the result in the form of integer. Since both the methods take in the same type and number of parameters and both return an integer, a delegate (called DeleMe) can be created, Which at times could be made made to point to SumMe() and at times to DiffMe(). When Delegate points to SumMe() and is called, the parameters passed would be added. However, if it is called while being pointed to DiffMe(),then the subtracted result of the parameters passed gets returned.

I think three steps involved in using delegates that are as follows:

1. Defining a delegate
Example: public delegate void DelegateName();

2. Instantiating a Delegate: means making it point or refer to some method. After a delegate is defined, it must be instantiated.
Example:
--------
public delegate int DeleMe(int a, int b);
-------

3. Using a Delegate: Means instantiating a method using delegates. A delegate is called in the same way as method is called. The only difference is that instead of calling the delegate implementation, the implementation code of the method associated with the delegate is called.
example:

class TestDelegates
{
public delegate int DeleMe(int a, int b);

class Maths
{
public int SumMe(int a, int b);
{
return a+b;
}
public int DiffMe(int a, int b);
{
return a-b;
}
}

class Test
{
static void Main()
{
DeleMe DelegateObj;
Maths m= new Maths();
DelegateObj = new DeleMe(m.SumMe);

int t=DelegateObj (5,3);
System.Console.WriteLine("The Result is:{0}",t);

}
}

I think its clear. so what will be the output.
output:
The Result is : 8
so how it will for difference? try in the code also and if problem tell me.

}




Responses

Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Author: John Fernandez    08 Jun 2008Member Level: Gold   Points : 1
Very well written Article.Thanks for sharing this information.


Author: antogladwin    17 Jun 2008Member Level: Silver   Points : 1
hi Ravindra,
it is very helpfull information 2 all.keep it up...continue posting Articles like this......

BY,
R.A.Gladwin


Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
Very Good Info for all of us.
thanks ton to you
Regards,
Kapil



Author: Gaurav Agrawal    18 Jun 2008Member Level: Silver   Points : 2
Hi Ravindra,
Nice article.
Your article is very helpful to understand the concept of delegate.
It was really confusing but it is clear now.
Please keep posting and help your fellow developers.
Happy programming.

Thanks
Gaurav Agrawal


Feedbacks      
Popular Tags   What are tags ?   Search 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: Side-By_side Execution In .net Framework
Previous Resource: How to display GridView Header Row Text
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

it support

Contact Us    Privacy Policy    Terms Of Use