A commission
1, the concept of
Delegate is a special class, which is stored one or more methods, these methods have the same signature and return type.
2, commissioned by use
the using the System; namespace ConsoleApp1 { class Program { static void the Main ( String [] args) { var Test = new new the Test (); // var = myDele new new MyDelegate (t.DoInstance); // declare a delegate mode. 1 MyDelegate myDele the Test = .DoStatic; // declare a delegate mode 2 myDele + = test.DoInstance; // add methods myDele + = Test.DoStatic; myDele - = Test.DoStatic; // removal methods //Delegate is invoked, calls the delegate and consistent method call, need to pass arguments, when the delegate is called, its associated method will be invoked sequentially myDele ( " the Hello World " ); the Console.ReadKey (); } } /// < Summary> /// declare a delegate /// </ Summary> /// <param name = "MSG"> </ param> public the delegate void MyDelegate ( String MSG); /// <Summary> /// test class / // </ Summary> public class the Test { // static method public static void DOSTATIC ( String msg) { Console.WriteLine($"DoStatic:{msg}"); } //实例方法 public void DoInstance(string msg) { Console.WriteLine($"DoInstance:{msg}"); } } }
3, anonymous methods
The method will only be used once to initialize the commission, without creating a method named private static methods of the current class is automatically created.
the using the System; namespace ConsoleApp1 { class Program { static void the Main ( String [] args) { // anonymous method is used to initialize delegate MyDelegate myDele = the delegate ( String MSG) { return $ " the Delegate: MSG {} " ; }; var retMsg myDele = ( " the Hello World " ); Console.WriteLine (retMsg); the Console.ReadKey (); } } /// <Summary> /// declare a delegate /// </ Summary> public the delegate String MyDelegate ( String MSG); }
4, Lambda expressions
Anonymous method to simplify, to simplify the process as follows:
the using the System; namespace ConsoleApp1 { class Program { static void the Main ( String [] args) { // anonymous method is used to initialize delegate MyDelegate myDele = the delegate ( String MSG) { return $ " the Delegate: MSG {} " ; }; // 1, the delegate is removed, add "=>" in parentheses after the parameter myDele = ( String MSG) => { return $ " the Delegate: MSG {} " ;}; //2, parameter types can be removed myDele = (MSG) => { return $ " the Delegate: MSG {} " ;}; // . 3, if only a number of parameters, can be removed "()" myDele = MSG => { return $ " the Delegate: MSG {} " ;}; // . 4, if the method is only a return statement inside of the body, remove the "return" and "{}" myDele = MSG => $ " the Delegate: MSG {} " ; var retMsg myDele = ( " the Hello World " ); Console.WriteLine (retMsg); the Console.ReadKey (); } } /// <Summary> /// declare a delegate /// </ Summary> public the delegate String MyDelegate ( String MSG); }
5 events
Event is a member of the class that encapsulates the delegate type of variable outside the class only allows "= +" and "- =" operator to register and unregister events. Declare an event but a similar statement made delegate type of package variables only.
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var test = new Test(); var eventTest = new EventTest(); eventTest.MyEvent += test.DoInstance; eventTest.MyEvent += Test.DoStatic; eventTest.MyEvent -= test.DoInstance; eventTest.OnEventHangler("Hello World"); //Where appropriate call the Console.ReadKey (); } } /// <Summary> /// declare a delegate /// </ Summary> /// <param name = "MSG"> </ param> public the delegate void MyDelegate ( String MSG); /// <Summary> /// test class /// </ Summary> public class the test { // static method public static void DOSTATIC ( String MSG) { Console.WriteLine ($ " DOSTATIC: {MSG } "); } // example of a method public void DoInstance ( String MSG) { Console.WriteLine ($ " DoInstance: MSG {} " ); } } /// <Summary> /// Event test class /// </ Summary> public class EventTest { /// <Summary> /// event is declared /// </ Summary> public event MyDelegate the MyEvent; /// <Summary> /// defined activation event process (activated from outside the class) // / </ the Summary> public Virtual void OnEventHangler ( String msg) { // event can only be carried out within the class declaration calling event where MyEvent? .Invoke (msg); } } }
6, and the inverter covariant
1), both of which use only the generic interface and generic delegate.
2), covariant: transition from the parent class subclasses direction direction, with key out, T type can only be used for the return value.
3), the inverter: direction shift from parent subclass direction, in use keywords, T type can only be used for incoming parameters.
7, built-in generic delegate system
1), Action generic delegates: 0-16 incoming parameters, no return value.
public delegate void Action(); public delegate void Action<in T>(T obj); public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2); public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3); ......
2), Func generic delegates: 0-16 arguments passed, a return value.
public delegate TResult Func<out TResult>(); public delegate TResult Func<in T, out TResult>(T arg); public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2); public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3); public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4); ......
3), Predicate generic delegate: an incoming parameters, return type bool.
public delegate bool Predicate<in T>(T obj);
4), Comparison generic delegate: an incoming type parameter takes two parameters of the same type used for comparison, return int.
public delegate int Comparison<in T>(T x, T y);