Defined:
A delegate is a type that holds a reference to a method in an object and is also a type-safe function pointer.
One way to understand a delegate is to think of the role of a delegate as specifying a name for the method signature.
The definition of a delegate is similar to the definition of a method, but there is no method body, and the keyword delegate is preceded by the defined delegate name.
Because defining a delegate is basically defining a new class, you can define the delegate anywhere in the definition class, either by defining the delegate inside another class, or by defining the delegate outside of all classes, and by defining the delegate as the top-level object in the namespace. Depending on the visibility of the definition, you can add generic access modifiers to the delegate definition: public, private, protected, and so on:
A delegate instance is compressed (or encapsulated) by a method called "a callable entity", so "a callable entity" is made up of this delegate instance itself and the methods encapsulated in the instance
A delegate can also contain multiple methods, which are called multicast delegates.
If you call a multicast delegate, you can call multiple methods sequentially consecutively. For this reason, the signature of the delegate must return void (otherwise, where should the return value be sent?). (When a delegate contains only one method, the declaration of its return type can refer to the encapsulated method, not necessarily void). In fact, if the compiler finds a delegate that returns void, it is automatically assumed to be a multicast delegate.
Advantages of Delegation:
(1) The Call of the compression method. (2) Reasonable and efficient use of the delegation can improve the performance of the application. (3) Used to invoke anonymous methods.
Declaration of delegate: scope modifier delegate delegate return type delegate name ();
such as: public delegate Void Test ();
Note: You can declare a delegate without a parameter or argument list. You should declare a delegate by following the same syntax as the method that declares it.
Sample Program for delegates:
Public delegate double Delegate_prod (int a, int b);
Class Class1
{
static double fn_prodvalues (int val1, int val2)
{
return val1 * VAL2;
}
static void Main (string[] args)
{
Creating the Delegate Instance
Delegate_prod DELOBJ = new Delegate_prod (fn_prodvalues);
Console.Write ("Please Enter Values");
int v1 = Int32.Parse (Console.ReadLine ());
int v2 = Int32.Parse (Console.ReadLine ());
Use a delegate for processing
Double res = DELOBJ (v1, v2);
Console.WriteLine ("Result:" + res);
Console.ReadLine ();
}
}
Explain:
Above I used a small program to demonstrate the use of delegates. A delegate Delegate_prod declaration specifies two return types that accept only integer variables. This is also true for methods named Fn_prodvalues in the same class, where delegates and methods have the same signature and parameter types. Create a delegate instance in the main method and pass the function name to the delegate instance as follows: Delegate_prod DELOBJ = new Delegate_prod (fn_prodvalues); This allows us to accept two values from the user and pass them to the delegate: DELOBJ (V1,V2); This delegate object compresses the functionality of the method and returns the result that we specified in the method.
Multicast delegation: (1) The multicast delegate contains a reference to more than one method. (2) The method that the multicast delegate contains must return void, or Run-time exception will be thrown.
Sample program for multicast delegates:
using System;
Using System.Collections.Generic;
Using System.Text;
Namespace ConsoleApp1
{
Declaring a delegate
public delegate void Msg ();
Write a class
Class Messges
{
A member method under the Messges class M1
public static void M1 ()
{Console.WriteLine ("Method one is Huashanlin called");}
A member method under the Messges class m2
public static void M2 ()
{Console.WriteLine ("Method Two is called by Huashanlin");}
A member method under the Messges class M3
public static void M3 ()
{Console.WriteLine ("Method 3huashanlin is called");}
}
Another class
Class Program
{
The main functions that are contained under this class
static void Main (string[] args)
{
Instantiate a delegate and encapsulate a method in the Messges class m2
Msg ms = new MSG (MESSGES.M2);
A new method is encapsulated in the original delegate instance that encapsulates a M1 method M1
ms = MS + MESSGES.M1;
Or the above statement can be written as MS + = MESSGES.M1; the effect is the same
The following encapsulates the third method for the delegate instance
Ms + = MESSGES.M3;
Invoking the delegate instance, the delegate is called after the multicast delegate has to execute the three methods in this wrapper
MS ();
}
}
}
Note: Deletge saved method signatures exist in the form of a hash queue
Multicast delegation Example program two:
delegate void Delegate_multicast (int x, int y);
Class Class2
{
static void Method1 (int x, int y) {
Console.WriteLine ("You r in Method 1");
}
static void Method2 (int x, int y) {
Console.WriteLine ("You r in Method 2");
}
public static void Main ()
{
Delegate_multicast func = new Delegate_multicast (METHOD1);
func + = new Delegate_multicast (METHOD2);
Func (n); Method1 and METHOD2 are called
Func-= new Delegate_multicast (METHOD1);
Func (2,3); Only METHOD2 is called
}Parsing: The above example program defines two accepted integer parameters named Method1 and Method2, and a method with a return type of void. Use the following declaration to create the delegate object in the main function: delegate_multicast func = new Delegate_multicast (METHOD1), and then use + = To add the delegate, using-= To remove the delegate.
C # Multicast delegation/Multicast Proxy