.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Delegates

Posted By: softraj     Posted Date: September 04, 2009    Points:2   Category :C#
Why delegates are used?

Responses
Author: Hefin Dsouza             
Posted Date: September 04, 2009     Points: 5   

Delegates are similar to pointers for Methods.
They are types that references to methods.
The delegate once initialised or assigned to a method starts behaving like the method itself.

The delegate can be also used to reference multiple methods.
Example :

public class Demo
{
public delegate int Calc(int a,int b);

static void Main()
{
Calc x=new Calc(Add);
x+=new Calc(Sub);
x(10,20);
}

static int Add(int a,int b)
{
return a+b;
}
static int Sub(int a,int b)
{
return a-b;
}
}

Will call both Add and Subtract by just a single call to x(object of the delegate Calc).

Regards
Hefin Dsouza



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend