标签:style blog http color io os ar 使用 for
概述:
除了为每个参数和返回类型定义一个新委托类型之外,可以使用Action<T> 和 Func<T> 委托.
Action<T>
Action<T>委托表示引用一个void返回类型的方法.
Func<T>
Func<T>委托允许调用带返回类型的方法.
使用:
和在 委托的使用 - 浅谈 中类似,我们可以使用允许带返回类型的方法的 Action<T> 委托:
1 using System; 2 3 namespace SimpleDelegates_Demo { 4 delegate double Operate(double input); 5 class Program { 6 static void Main(string[] args) { 7 Func<double, double>[] actions = {MathOperations.Square, MathOperations.MultiplyByTwo }; 8 //遍历每个委托实例. 9 foreach (Func<double, double> action in actions) { 10 ProcessAndDisplayResult(action, 2); 11 ProcessAndDisplayResult(action, 2.5); 12 ProcessAndDisplayResult(action, 5.2); 13 Console.WriteLine(); 14 } 15 } 16 17 static void ProcessAndDisplayResult(Func<double, double> action, double inputVal) { 18 Console.WriteLine("Input is [{0}],Result is [{1}]", inputVal, action(inputVal)); 19 } 20 } 21 }
标签:style blog http color io os ar 使用 for
原文地址:http://www.cnblogs.com/listened/p/4051135.html