INTRODUCTION:-
Delegates is variable type that allow you to use function indirectly. The some delegates can be used to call any function that matches a specific signature., giving you ability to choose between several function during the run time.
Delegates are declare much like function but with no function body and using the delegates keyword. The delegate declaration specified a function signature consisting of a return type and the parameter list. Delegates same as pointer to function in C and C++. Class declaration is then necessary condition for implementing delegates.
WHY DELEGATE?
Delegate improve the application performance.
Delegate use for encapsulation to the method calling from user.
Its use to call method asynchronously with in program. Delegates commonly used for Multithreading and Event handling.
SYNTAX:-
delegate <return type> delegate_name <parameter param_name>;//param is optional
Create object of Delegates
After delegate type has been declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the new expression is written like a method call, but without the arguments to the method. For example:
delegate double process_delegate(double num1, double num2); process_delegate pd1 = new process_delegate(Add); process_delegate pd2 = new process_delegate(Mult);
Program for Delegate:-
using System; namespace C#DelegatesDemo { class delegate_program { delegate double process_delegate(double num1, double num2);; // Declare the delegate. //Method for addition Static double Add(double num1, double num2) { return num1 + num2; } //Method for multiplication Static double Mult(double num1, double num2) { return num1 * num2; } Static Void mail (String [] args) { process_delegate process; // object creation for delegate. Console.WriteLine("Enter the choice, A for Addition and M for multiplication?"); String input = Console.ReadLine(); // Read the user choice. if(input.ToUpper()=="A") process = new process_delegate(delegate_program.Add); // Create delegate instance. Else process = new process_delegate(delegate_program.Mul); // Create delegate instance. Console.WriteLine("Enter two numbers"); double a = convert.ToDouble(Console.ReadLine()); double b = convert.ToDouble(Console.ReadLine()); Console.WriteLine("Result={0}",process(a,b)); // Calling the delegate. COnsole.ReadLine(); //Prevent the screen for vanish. } } }
OUTPUT:-
Enter the choice, A for Addition and M for multiplication A Enter to numbers 20 10 Result=30
Multicasting of a Delegate
Multicasting means delegates inside delegates. means using single object we can call two or more delegates by passing delegates object as a parameter. let me explain by example:-
using System; delegate int process_delegate(int val); namespace C#DelegatesDemo { class delegate_program { static int num = 10; public static int Add(int val1) { return num+=val1; } public static int Mult(int val2) { return num*=val2; } public static int printval() { return num; } static void Main(string[] args) { process_delegate pd; //create delegate object process_delegaten pd1 = new process_delegate(Add); // Create delegate instance. process_delegaten pd2= new process_delegate(Mult); // Create delegate instance. pd = pd1;//assign pd1 delegates object pd += pd2;//Delegate objects can be composed using the "+" operator. //calling multicast delegates pd(20);//it will call both delegates (invoke both method) Console.WriteLine("Value of Val: {0}", printNum()); COnsole.ReadLine(); //Prevent the screen for vanish. } } }
OUTPUT:-
Value of Val:600
CONCLUSION:-
This article would help to understand delegates in C# using simple examples. If you have any query regarding this article, you can reach me by comment here.