What are the delegates in c# (dotnet)?
Delegates are similar to object references, but are used to reference methods instead of objects. The type of a delegate is the type or signature of the method rather than the class. Hence following are the three properties of delegates:
- The type or signature of the method that the delegate can point to
- The delegate reference which can be used to reference a method
- The actual method referenced by the delegate
At Very first advantage of the delegate is that it encapsulate our method so the user of our application is don't know about the inner processing of the application. The other advantages are in the following:
- Effective use of delegate improves the performance of application
- Used to call a method asynchronously
Declaration of a delegate is in the following:
delegate datatype delegate_name(list of parameters);
delegate: keyword
datatype: type of the delegate which must be same as the type of function to whom the delegate is refering.
list of parameters: all the parameters same as the parameters of the function to whom the delegate is refering.
Example for syntax:
delegate int myDelegate(int a, int b);
Once we have defined a delegate type, we can set it to reference actual methods with matching signatures. A delegate reference can be declared just like an object reference. For example, a reference of type myDelegate (defined above) can be declared as:
myDelegate arithDelegate;
Now, the delegate reference arithDelegate can reference any method whose signature is identical to the signature of myDelegate.
The delegate reference can be made to reference any method with a matching signature by passing its name as the parameter of delegate:
myDelegate arithDelegate = new myDelegate(function_name);
For calling our or passing the parameters to our actual function we can do some following stuff.
int r = arithDelegate(list of parameters);
Now, we have done each and every thing for the delegate. So, let's take an example to add two numbers as in the following:
Example: adding two numbers
using System;
namespace cSharpDotNet
{
class Program
{
namespace cSharpDotNet
{
class Program
{
//Declaring our delegate
delegate int arithMethod(int a, int b);
//Function definition to whom delegate will reference
delegate int arithMethod(int a, int b);
//Function definition to whom delegate will reference
private static int add(int a, int b)
{
return a + b;
}
static void Main(string[] args)
{
{
return a + b;
}
static void Main(string[] args)
{
//initializing object of our delegate and also referring to the function
arithMethod obj = new arithMethod(add);
arithMethod obj = new arithMethod(add);
//Passing parameters to the delegate
int num = obj(4,5);
Console.WriteLine("The result of arithmetic operation '+' on 3 and 4 is :{0}", num);
Console.ReadLine();
}
}
}
Console.WriteLine("The result of arithmetic operation '+' on 3 and 4 is :{0}", num);
Console.ReadLine();
}
}
}
after building solution and debugging the above code this will show the following output on to the black screen.
That's all from today.
Thank you! ! !
nice . and thanks man I was searching a lot about delegates but I couldn't find satisfactory answer. Today you solved my problem by providing easy and friendly discussion.
ReplyDeleteNice and easy Explanation to understand Delegates for Beginer... good keep it up..
ReplyDeletenice post bro... keep it up..
ReplyDeleteGood Explanation.. V nice. Keep it up... :)
ReplyDelete