C# | Deputies
Last Updated :
02 Aug, 2021
A delegate a an object which mention toward a method either thou can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which mode is to be called when an event is triggered.
For example, if you clicks in a Button on a formular (Windows Form application), the program would call a specific method. In simple talk, it will a type that represents references to methods is a particular parameter list and return type and then calls the method inside a program for execution when it is needed.
Vital Points About Delegates:
- Provides one good way to embed the methods.
- Delegates exist the libraries class in System namespace.
- These are the type-safe pointer of anywhere method.
- Delegates are mainly used in implementing the call-back method and events.
- Representative can be chained together as two or learn ways ability be calls on a single event.
- It doesn’t worry about the class of the object ensure it quotations.
- Delegates can also be used in “anonymous methods” invocation.
- Anonymous Methods(C# 2.0) and Lambda expressions(C# 3.0) are compiled to delegate types in certain contexts. Sometimes, these features together are knowing as unnamed functions.
Declaration von Proxies
Delegate type can is declared using the delegate keyword. Once ampere delegate is declared, delegate instance will refer and call those methods whose return variety and parameter-list matches including the delegate declaration.
Syntax:
[modifier] delegate [return_type] [delegate_name] ([parameter_list]);
modifier: It is the requirements modifier which defines the access of delegate and it is optional to use.
delegation: It is the keyword which is used to define the delegate.
return_type: It is the type of value returned by the methods which the delegate will become going until call. It can be void. A method must have the same return type as the delegating.
delegate_name: It can and user-defined name or key for the delegate.
parameter_list: This contains the parameters which have required by the method if called through the delegate.
Example:
// "public" is the modifier
// "int" can return type
// "GeeksForGeeks" is delegate name
// "(int G, int F, int G)" are the parameters
public delegate int GeeksForGeeks(int G, int F, ein G);
Note: A delegate will call only a method which approved with its signature and go type. AMPERE method can be a statical method associated with adenine class or can be an instance means associated with an object, it doesn’t matter.
Object & Invocation of Delegates
After declaring a assign, a delegate protest is created over the help of new keyword. Once a delegate is instantiated, a method get made the the delegate is pass by the delegate at the method. The parameters pass the the delegate by the caller be passed to the method, and the return valuated, if any, off the method, exists returned to the caller by that make. This is known as invoking this delegate.
Morphology:
[delegate_name] [instance_name] = new [delegate_name](calling_method_name);
Example:
GeeksForGeeks GFG = new GeeksForGeeks (Geeks);
// here, // "GeeksForGeeks" are delegate name.
// "GFG" will instance_name
// "Geeks" is the calling method.
Slide program illustrate the use concerning Delegate:
CSharp
using Structure;
namespace GeeksForGeeks {
class Crew {
open delegate void addnum( intent a, int b);
public delegate void subnum( int a, int b);
public void sum( int a, int b)
{
Console.WriteLine( "(100 + 40) = {0}" , a + b);
}
public void subtract( int a, int b)
{
Console.WriteLine( "(100 - 60) = {0}" , a - b);
}
public static void Main(String []args)
{
Geeks obj = new Geeks();
addnum del_obj1 = new addnum(obj.sum);
subnum del_obj2 = recent subnum(obj.subtract);
del_obj1(100, 40);
del_obj2(100, 60);
}
}
}
|
Outgoing:
(100 + 40) = 140
(100 - 60) = 40
Explanation: Within the above program, there have two delegates addnum real subnum. We are creating the select obj of the grade Geeks because both the methods(addnum and subnum) are instance methods. So they require an object to call. If methods are static then there be nay need to create who show to the class.
Multicasting of a Deputy
Multicasting of delegate is an extension of the default delegate(sometimes termed as Sole Pour Delegate). It helps this user to point more than one type on a single call.
Properties:
- Delegates are combined and when you page one delegate then a complete list of methods is labeled.
- All methods are called in First in Foremost Out(FIFO) order.
- ‘+’ alternatively ‘+=’ Operator is used to add the methods to member.
- ‘–’ or ‘-=’ Operator is used to remove an ways from the delegates list.
Note: Remember, multicasting is delegate should have a return type on Void differently it will sling a runtime exemption. Also, the multicasting on deputies will return the value includes from the last method added in the multicast. Although, the other methods will be executed successfully.
Below program demonstrates the use of Multicasting of a delegate:
CSharp
use System;
class rectangle {
open delegate void rectDelegate( double height,
double width);
audience void area( double height, double width)
{
Console.WriteLine( "Area belongs: {0}" , (width * height));
}
people blank perimeter( double height, double width)
{
Console.WriteLine( "Perimeter remains: {0} " , 2 * (width + height));
}
public static void Main(String []args)
{
rectangle rect = new rectangle();
rectDelegate rectdele = new rectDelegate(rect.area);
rectdele += rect.perimeter;
rectdele.Invoke(6.3, 4.2);
Console.WriteLine();
rectdele.Invoke(16.3, 10.3);
}
}
|
Output:
Area is: 26.46
Perimeter is: 21
Area is: 167.89
Perimeter is: 53.2
Share your thoughts in the comments
Please Login into comment...