namespace out_ref { //委托匿名方法 public delegate void Myadd(); class Program { static void Main(string[] args) { Say(delegate() { Console.WriteLine("哈哈,就是这么帅!"); }); Console.ReadKey(); } static void Say(Myadd myadd) { myadd(); } } }
//委托匿名方法 求两个数之和 public delegate int Myadd(int n1,int n2); class Program { static void Main(string[] args) { int a = Sum(delegate(int n1, int n2) { return n1 + n2; }); Console.WriteLine(a); Console.ReadKey(); } static int Sum(Myadd myadd) { return myadd(10,20); } }
//委托匿名方法 求两个数之和(2) public delegate int Myadd(int n1, int n2); class Program { static void Main(string[] args) { Myadd add = delegate(int n1, int n2) { return n1 + n2; }; int sum = add(10, 20); Console.WriteLine(sum); Console.ReadKey(); } }
时间: 2024-11-09 03:22:16