In this post we will discuss about delegates in C#.net.
Also check out:
- Delete all stored procedures at once in SQL Server database
- Caching in Asp.Net
- How to add Eval for hyperlink in gridview in asp.net?
- Delegates allow you to create a variable that points to a method. Then use this variable at any time to invoke the method.
- First define the signeture like the method must have the same return type, the same number of parameters, and the same data type for each parameter as the delegate.
- A delegate variable can point only to a method that matches its specific signature. The method must have the same return type, the same number of parameters, and the same data type for each parameter as the delegate.
- private string GetMeaning(string word)
{
}
This method accepts a single string argument and returns a string.
Now declare the delegate as like below:
Syntax:
public delegate <Return type> delegate name(parameters)
private delegate string MyFunction(string inputString);
Now in the next step we have to declare the variable like below:
MyFunction myDelegateVariable;
myDelegateVariable = GetMeaning;
Now you can invoke the method through the delegate like below:
string strValue;
strValue = myDelegateVariable("AspDotNetHelp.com");
Multicast Delegate:
It is a delegate which holds the reference of more than one method.
Multicast delegates must contain only methods that return void, else there is a run-time exception.
Also check out:
- Delete all stored procedures at once in SQL Server database
- Caching in Asp.Net
- How to add Eval for hyperlink in gridview in asp.net?
- Delegates allow you to create a variable that points to a method. Then use this variable at any time to invoke the method.
- First define the signeture like the method must have the same return type, the same number of parameters, and the same data type for each parameter as the delegate.
- A delegate variable can point only to a method that matches its specific signature. The method must have the same return type, the same number of parameters, and the same data type for each parameter as the delegate.
- private string GetMeaning(string word)
{
}
This method accepts a single string argument and returns a string.
Now declare the delegate as like below:
Syntax:
public delegate <Return type> delegate name(parameters)
private delegate string MyFunction(string inputString);
Now in the next step we have to declare the variable like below:
MyFunction myDelegateVariable;
myDelegateVariable = GetMeaning;
Now you can invoke the method through the delegate like below:
string strValue;
strValue = myDelegateVariable("AspDotNetHelp.com");
Multicast Delegate:
It is a delegate which holds the reference of more than one method.
Multicast delegates must contain only methods that return void, else there is a run-time exception.