Commissioned
1. Concept:
A delegate can be thought of as a "container" of a function, and it can be used as a function after "loading" a specific function. A delegate variable can be thought of as a type-safe function pointer that can receive only the address of a function that meets its requirements.
2, definition method: Delegate keyword. Cases:
Public Delegate int Mydele (intint b);
3, using the delegate call function
1) Define a class first and write a few methods:
class Class1 { publicint jiafa (intint b) { return A + b; } Public int JIANFA (intint b) { return A- b; }
}
2) Call the function through the delegate:
//Define a variable of an existing delegate type New //Assign values to the above variables Console.WriteLine (M (1,2)); The result is 3new Class1 (). JIANFA; Console.WriteLine (M (1,2)); //result is-1
4. Note: A variable of a delegate type can refer to any function that satisfies its requirements.
5, the extension of the delegation:
1) Special delegate generic set, anonymous delegate
func<stringstringnew Class1 (). returnstring; // input string, output string Console.WriteLine (MyFunc (" Zhang San "));
2) Direct Write method
func<stringstringdelegate(string s) { return] world! " ; }; Console.WriteLine (Newfunc (" John Doe "));
3) Lambda method simple version 1, replace delegate with s=> (string s)
func<stringstring> newfunc1 = s=> { return'world! "
4) lambda method simple version 2, two parameters
func<stringstring,string> Newfunc2 = (s1,s2) + = { return " world! "+s2;
5) Lambda method simple version 2, remove curly braces, direct write method
func<stringstring,string> newfunc3 = (S1, s2) =>s1+"world! "+s2;
Console.ReadLine ();
7. Simple application of object-oriented and WinForm (entrustment)