c#中三类系统委托声名

Action、Func、Predicate

Action表示接受0个或者多个输入参数,执行一段代码,返回值为空 

 1 private void AddAction(int a, int b)
 2     {
 3         Console.WriteLine(a + b);
 4     }
 5     static void Main(string[] args)
 6     {
 7         TestCSharpClass myTest = new TestCSharpClass();
 8         Action<int, int> testAction = myTest.AddAction;
 9         testAction(1, 2);
10     }

 

Func表示接受0额或多个输入,带返回值

private int AddFuncTest(int a, int b)
    {
        return a + b;
    }
    static void Main(string[] args)
    {
        TestCSharpClass myTest = new TestCSharpClass();
        Func<int, int, int> funcTest = myTest.AddFuncTest;
        Console.WriteLine(funcTest(1,2));
    }
Func

 

predicate表示定义一组条件并返回是否满足条件 bool值

 1 private class PreTestClass
 2     {
 3         private int a;
 4         private int b;
 5         public PreTestClass(int a0, int b0)
 6         {
 7             a = a0;
 8             b = b0;
 9         }
10         public bool TestBool(int ccc)
11         {
12             if (a + b >= ccc)
13             {
14                 return true;
15             }
16             return false;
17         }
18     }
19     static void Main(string[] args)
20     {
21         PreTestClass testClass = new PreTestClass(1, 2);
22         Predicate<int> testPredicate = testClass.TestBool;
23         Console.WriteLine(testPredicate(4));
24     }

 

转载于:https://www.cnblogs.com/yuyangyang/p/11356223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值