代理delegate到lamda的演化示例

本文通过具体的C#代码示例介绍了如何使用委托及Lambda表达式进行方法的传递与调用,展示了从传统委托定义到简洁Lambda语法的演变过程。
using System;

namespace ConsoleApp1
{
    public class Program
    {
        private delegate int Add(int a, int b);
        public static void Main(string[] args)
        {
            Add add1 = new Add(AddPlus);
            int z = add1(2, 6);
            Console.WriteLine(z);

            add1 = new Add(delegate (int a, int b) { return a + b; });
            z = add1(2, 6);
            Console.WriteLine(z);


            Func<int, int, int> add2 = new Func<int, int, int>(delegate (int a, int b) { return a + b; });
            Console.WriteLine(add2(1, 9));

            add2 = delegate (int a, int b) { return a + b; };
            Console.WriteLine(add2(18, 9));

            add2 = (int a, int b) => { return a + b; };
            Console.WriteLine(add2(15, 9));

            add2 = (a, b) => { return a + b; };
            Console.WriteLine(add2(15, 9));

            add2 = (a, b) => a + b;
            Console.WriteLine(add2(11, 9));


            Console.Read();
        }


        public static int AddPlus(int a, int b)
        {
            return a + b;
        }

        public static int Reduce(int a, int b)
        {
            return a - b;
        }

    }
}

 

转载于:https://www.cnblogs.com/slwangzi/p/6112152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值