委托
1
class Program { private delegate string GetAStr(); static void Main(string[] args) { //int x = 40; //string s = x.ToString(); //Console.WriteLine(s); //GetAStr getAStr = new GetAStr(x.ToString); //Console.WriteLine(getAStr.Invoke()); PrintStr method = Method1; Printf(method); method = Method2; Printf(method); Console.ReadKey(); } private delegate void PrintStr(); static void Printf(PrintStr print) { print(); } static void Method1() { Console.WriteLine("method1"); } static void Method2() { Console.WriteLine("method2"); } }
2 action
class Program { static void PrintStr(int i) { Console.WriteLine(i); } static void PrintStr(string s) { Console.WriteLine(s); } static void PrintStr(string s, int i) { Console.WriteLine(s); } static void Main(string[] args) { //int x = 100; //Action a = PrintStr; //Action<int> a = PrintStr; Action<string, int> a = PrintStr; } }
3 func
class Program { static int Test1() { return 1; } static int Test2(string s) { //Console.WriteLine(s); return 100; } static void Main(string[] args) { Func<string,int> a = Test2; Console.WriteLine(a("sad")); Console.ReadKey(); } }