What is Delegate in C#.
Delegate is an object which holds the references to a method within the delegate object.It is type safe object and invoking the method asynchronously manner.
Advantage of Delegates
1 Improved the performance of Application
2 Call a method asynchronous.
Type of Delegate
1. Single Delegate and
2 Multicast Delegate
3 Generic Delegate
Single Delegate :-A delegate is need to pass single parameter as reference to a method with the delegate object.
https://msdn.microsoft.com/en-us/library/system.delegate.aspx
Multicast Delegate: - When we need to pass more than one parameter as reference to a method within delegate object.
Delegate is an object which holds the references to a method within the delegate object.It is type safe object and invoking the method asynchronously manner.
Advantage of Delegates
1 Improved the performance of Application
2 Call a method asynchronous.
Type of Delegate
1. Single Delegate and
2 Multicast Delegate
3 Generic Delegate
Single Delegate :-A delegate is need to pass single parameter as reference to a method with the delegate object.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace DelegateTest
{
public delegate void Calc(int x, int y);
{
public delegate void Calc(int x, int y);
class A {
public void add(int x, int y)
{
Console.WriteLine("The Sum is " + (x + y));
}
public void add(int x, int y)
{
Console.WriteLine("The Sum is " + (x + y));
}
public void sub(int x, int y)
{
Console.WriteLine("The Difference is " + (x - y));
Console.ReadLine();
}
}
{
Console.WriteLine("The Difference is " + (x - y));
Console.ReadLine();
}
}
class Program {
static void Main(string[] args)
{
A obj = new A();
//make an object of class A
Calc objsum= new numtest(obj.add);
objsum(10, 20);
Calc objsub= new numtest(obj.sub);
objsub(10, 20);
}
}
}
static void Main(string[] args)
{
A obj = new A();
//make an object of class A
Calc objsum= new numtest(obj.add);
objsum(10, 20);
Calc objsub= new numtest(obj.sub);
objsub(10, 20);
}
}
}
https://msdn.microsoft.com/en-us/library/system.delegate.aspx
Multicast Delegate: - When we need to pass more than one parameter as reference to a method within delegate object.