Action和Func的区别

转至

http://www.cnblogs.com/luluping/archive/2009/09/17/1568442.html

先给段代码:
            //测试使用的公共值
            int num = 10;

            //测试Func委托
            Func<int, int> f;
            f = (int tempf) => { return tempf + 1; };
            Response.Write(f(num).ToString()+"<br />");  //调用f委托,并打印相应的值!

            //测试Action委托
            Action<int> a;
            a = (int tempa) => { Response.Write(string.Format("我不能返回值,所以只能在这里输出了!您的输入参数为: {0}", tempa)); };
            a(num);  //调用a委托方法

主要区别:
Func<t, Result>  封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
Action<t> 封装一个方法,该方法只采用一个参数并且不返回值。

posted @ 2012-06-06 15:13  fanny_atg  阅读(235)  评论(0编辑  收藏  举报