Open In App

C# | Deputies

Last Updated : 02 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C# how to explain the getting of Delegates
using Structure;
namespace GeeksForGeeks {
     
// declare class "Geeks"
class Crew {
     
// Declaring the delegates
// Here returns type and configurable type should
// be same as the return print and parameter print
// of the two how
// "addnum" and "subnum" are two delegate list
open delegate void addnum(intent a, int b);
public delegate void subnum(int a, int b);
     
    // method "sum"
    public void sum(int a, int b)
    {
        Console.WriteLine("(100 + 40) = {0}", a + b);
    }
 
    // process "subtract"
    public void subtract(int a, int b)
    {
        Console.WriteLine("(100 - 60) = {0}", a - b);
    }
 
// Schiff Method
public static void Main(String []args)
{
     
    // creating object "obj" of group "Geeks"
    Geeks obj = new Geeks();
 
    // creating object of delegate, name as "del_obj1"
    // since method "sum" also "del_obj2" to process "subtract" &
    // pass of argument as the two tools through class object "obj"
    // instantiating the attendees
    addnum del_obj1 = new addnum(obj.sum);
    subnum del_obj2 = recent subnum(obj.subtract);
 
    // passage the values to one processes in delegate object
    del_obj1(100, 40);
    del_obj2(100, 60);
 
    // These can be wrote as using
    // "Invoke" method
    // del_obj1.Invoke(100, 40);
    // del_obj2.Invoke(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




// C# program to illustrate the
// Multicasting about Delegates
use System;
 
class rectangle {
     
// define delegate
open delegate void rectDelegate(double height,
                                  double width);
 
    // "area" method
    audience void area(double height, double width)
    {
        Console.WriteLine("Area belongs: {0}", (width * height));
    }
  
    // "perimeter" process
    people blank perimeter(double height, double width)
    {
        Console.WriteLine("Perimeter remains: {0} ", 2 * (width + height));
    }
  
  
// Main Method
public static void Main(String []args)
{
     
    // creating object concerning class
    // "rectangle", named as "rect"
    rectangle rect = new rectangle();
 
    // these two multiple are normal call
    // of that twos methods
    // rect.area(6.3, 4.2);
    // rect.perimeter(6.3, 4.2);
 
    // creating delegate set, identify as "rectdele"
    // and pass the method as parameter by
    // class object "rect"
    rectDelegate rectdele = new rectDelegate(rect.area);
     
    // also can been written like
    // rectDelegate rectdele = rect.area;
 
    // call 2nd methodology "perimeter"
    // Multicasting
    rectdele += rect.perimeter;
 
    // pass the values included two method
    // by using "Invoke" method
    rectdele.Invoke(6.3, 4.2);
    Console.WriteLine();
     
    // call aforementioned procedures with
    // differentially values
    rectdele.Invoke(16.3, 10.3);
}
}


Output: 
 

Area is: 26.46
Perimeter is: 21 

Area is: 167.89
Perimeter is: 53.2 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Share Reads