.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 >> Code Snippets >> Performace >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Delegates

Posted By:nishithraj       Posted Date: November 27, 2009    Points: 10    Category: Performace    URL: http://www.dotnetspark.com  
 

Delegate is a function pointer available in dotnet. It is type-safe to use.
Events use delegate for their functionalities. Delegates place important roles in asynchronous programming. We can do multi casting in delegates.

A sample code for delegates
 
//delegate return type and arguments should match with the function.
public delegate void NumberHandle();
public class Numbers{
private int num=70;
public NumberHandle LargeNum;
public NumberHandle NegativeNum;
public NumberHandle NumChanged;
public NumberHandle ZeroValue;

public int Num{
get{
return num;
}
set{
if(value==0)
return;
//csharp way of coding using delegate
num+=value;
if (null!=LargeNum && num>100)
LargeNum();
if (null!=NegativeNum && num<0)
NegativeNum();
if (null!=ZeroValue && num==0)
ZeroValue();
}
}
}


Syntax of VB.NET to call the delegate is different.
i.e, by using the Addressof keyword.


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

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