码迷,mamicode.com
首页 > 其他好文 > 详细

Action<T> 和 Func<T> 委托

时间:2014-10-26 00:21:33      阅读:198      评论:0      收藏:0      [点我收藏+]

标签: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 }

 

  

Action<T> 和 Func<T> 委托

标签:style   blog   http   color   io   os   ar   使用   for   

原文地址:http://www.cnblogs.com/listened/p/4051135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!