c#中自定义泛型委托实现Action和Func的实例比较及lambda表达式写法

73 篇文章 3 订阅
46 篇文章 0 订阅
本文详细介绍了C#中Lambda表达式的使用,包括自定义委托Action和Func,以及如何将它们封装成事件,以便注册并调用多个方法。还展示了Action和Func的多种重载形式以及泛型委托的应用。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LambdaTest
{
    public class LambdaCreate
    {
        //自定义委托(Action和Func实质上是封装好的委托)
        //将委托封装成事件来注册,则可以一个事件注册多个同类型方法(事件执行将调用全部已注册过的方法)
        delegate void delegate_action1();
        event delegate_action1 delegate_action1_event;

        delegate void delegate_action2(object a,object b);
        event delegate_action2 delegate_action2_event;

        delegate object delegate_func1(object a);
        event delegate_func1 delegate_func1_event;

        delegate object delegate_func2(object a,object b);
        event delegate_func2 delegate_func2_event;

        //Action和Func实质上是封装好的委托
        //自定义泛型委托(多写几个重载的话可替代Action和Func)
        delegate void delegate_func3<T>(T a, T b);
        delegate T delegate_func4<T>(T a, T b);
        public void Test()
        {
            //除事件外,被定义封装的委托可当成参数传递使用
            //有16个函数重载,最多的有16个形参
            Action action1 = () => { int c = 8 + 9; };
            Action<int, int> action2 = (int a, int b) => { int c = a + b; };
            Action<object, object> action3 = (object a, object b) => { a = b; };
            //有16个函数重载,最多的有16个形参
            Func<int> func1 = () => { int c = 1; return c; };//Fun至少要有一个返回参数,输入形参可没有
            Func<int, int> func2 = (int a) => { int c = a; return c; };
            Func<object, object> func3 = (object a) => { object c = a; return c; };//最后一个形参是返回类型
            //自定义委托,可替代Action和Func(Action和Func实质上是封装好的委托)
            delegate_action1_event += new delegate_action1(() => { int c = 8 + 9; });   
            delegate_action2_event += new delegate_action2((object a,object b) => { object c = a; c = b; });
            delegate_func1_event += new delegate_func1((object a) => { object c = 9;return c; });
            delegate_func2_event += new delegate_func2((object a,object b) => { object c = a; return c; });
            //以上部分方法的lambda表达式写法
            action1 = new Action(() => { int c = 8 + 9; });
            action3 = new Action<object,object>((object a, object b) => { a = b; });
            func1 = new Func<int>(()=> { int c = 1; return c; })  ;
            func3 = new Func<object, object>((object a) => { object c = a; return c; });

            delegate_action1 normal_delegate_action1 = new delegate_action1(() => { int a = 9; });
            delegate_action2 normal_delegate_action2 = new delegate_action2((object a,object b) => {  a = b; });
            delegate_func1 normal_delegate_func1 = new delegate_func1((object a) => { object c = a; return c; });
            delegate_func2 normal_delegate_func2 = new delegate_func2((object a,object b) => { object c = a; return b; });

            normal_delegate_action1 = () => { int a = 9; };
            normal_delegate_action2 = (object a, object b) => { a = b; };
            normal_delegate_func1 = (object a) => { object c = a; return c; };
            normal_delegate_func2 = (object a, object b) => { object c = a; return b; };

            delegate_action1_event += () => { int c = 8 + 9; };
            delegate_action2_event += (object a, object b) => { object c = a; c = b; };
            delegate_func1_event += (object a) => { object c = 9; return c; };
            delegate_func2_event += (object a, object b) => { object c = a; return c; };
            //泛型声明的时候是T,定义实例的时候需要填具体的参数类型(而不是T或T1,T2)
            delegate_func3<int> normal_delegate_func3 = new delegate_func3<int>((int a,int b) => { int c = a + b; });
            delegate_func4<object> normal_delegate_func4 = new delegate_func4<object>((object a, object b) => { object c = a; return b; });
        }
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值