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



My Profile

Gifts

Active Members
TodayLast 7 Days more...







About Delegates and Anonymous methods in C#


Posted Date: 28 Jul 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Himasagar Kutikuppala       Member Level: Silver
Rating:     Points: 10




Delegate


Delegate is similar to function pointer in C and C++. A function pointer in C is a variable that points to function. Similarly, a delegate is a reference type in .net that is used to call the methods of similar signature as of the delegate. delegates are type-safe, object oriented.

Types of Delegate:

Single cast delegate

SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class. In single-cast delegates, the delegate can point to both static and non-static method if both have the same signature as the delegate

simple delegate with no return type and taking two arguments as parameters.

public delegate void MyMethod1(string s ,int i);


simple delegate that returns a value.

public delegate bool MyMethod2());


Small Example:

// Decalre Delegate which will point to a method which takes string as parameter and returns string as well
public delegate string MyOwnDelegate(string message);
// Decalre Delegate which will point to a method which takes 2 integer parameters and returns integer as well
public delegate int MyOwnAddDelegate(int a, int b);

//The functions to which delegates pointing
public string MessageMethod(string name)
{
MessageBox.Show(name);
return "Hi" + name;
}
public int AddNumber(int a, int b)
{
return (a + b);
}

// We have created an instance of our MyOwnDelegate & MyOwnAddDelegate

MyOwnDelegate myStringDelegate = new MyOwnDelegate(MessageMethod);
MyOwnAddDelegate myAddDelegate = new MyOwnAddDelegate(AddNumber);

// when we call the delegate it calls the method which it is pointing to
string result1 = myStringDelegate("Hello");
int result2 = myAddDelegate(10,20);


Multi cast delegate

A delegate is called Multi-cast Delegate that derives from the System.MulticastDelegate contains an invocation list with multiple methods.

public delegate void MyMulticastDelegate(string name);

public void DisplayFirstName(string firstName)
{
MessageBox.Show(firstName);
}
public void DisplaySecondName(string secondName)
{
MessageBox.Show(secondName);
}

MyMulticastDelegate myDelegate = new MyMulticastDelegate(DisplayFirstName);
myDelegate += new MyMulticastDelegate(DisplaySecondName);
myDelegate("Hima Sagar");


Anonymous methodS
anonymous method is a method without any name.Anonymous methods can be used in the place where there is a use of a delegate.For example there is a mthod, wouldn’t be called by any other code other than the delegate itself then we can make use of anonymous method i.e. method with no name.

public delegate string MyOwnDelegate(string message);
public delegate int MyOwnAddDelegate(int a, int b);

public MyOwnDelegate myStringDelegate = delegate(String name)
{
MessageBox.Show(name);
return "Hi" + name;
};

public MyOwnAddDelegate myAddDelegate = delegate(int a, int b)
{
return (a + b);
};

string result3 = myStringDelegate("Hello");
int result4 = myAddDelegate(10, 20);


For more details please go through the following websites
http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms173172(VS.80).aspx


Himasagar Kutikuppala






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Tutorial on Delegates  .  Tutorial on Anonymous methods  .  Delegates in C#  .  Delegates and Anonymous methods in C#  .  Delegates and Anonymous methods  .  Delegate and anonymous method  .  Anonymous methods in C#  .  

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: How to Create and Use a simple Custom Control
Previous Resource: Copy directory recursively in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween mask

Contact Us    Privacy Policy    Terms Of Use