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)); }
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 }