匿名函数和lambda表达式

//delegate 返回值类型  委托名(参数列表);
    public delegate void ActionHandler();
    class Demo0713 {
        public delegate string Action5();
        public delegate string Action3(int j);
        public delegate void Action4(int j,int i);
        public void Test() {
           // Test2();//调用方法执行里面代码
            ActionHandler a = delegate() {
                Console.WriteLine("我是一个匿名函数");
            };
            a();
            Action3 act = delegate(int j)
            {
                Console.WriteLine("我是一个匿名函数");
                return "a";
            };
            string name=act(12);
            act = Test2;
        }
        public static void Main() {
            Action3 act = delegate(int j)
            {
                return "a";
            };
            //当方法体代码只有一行时,可以省略{},甚至省略return;s
            Action3 act2 = j =>"a";
            string str = act2(12);
            Console.WriteLine(str);


            Action3 act1 = delegate(int j)
            {
                j = j + 2;
                return j + "-" + j;
            };
            //当方法体代码超过一行不可以省略{};
            Action3 act3 = j => { j = j + 2; return j + "-" + j; };
            Action4 act4 = delegate(int i, int j)
            {


            };
            //当超过两个参数时,不可以省略();
            Action4 act5=(i,j)=>Console.WriteLine(123);
            Action5 act6 = () => "a";
            ActionHandler a=()=>Console.WriteLine(12);
        }
        public string Test2(int i) {
            return "abc";
        }
        public static void Test3() { 
        
        }

猜你喜欢

转载自blog.csdn.net/qq_36499416/article/details/81027570