Action 和 Func 的用法以及区别

 

Action 无返回值

Func 有返回值,且最后一个参数为返回值

 

Action用法

 

public static void test(string s)
        {
            Console.WriteLine("name1:{0}", s); 
        }
static void Main(string[] args)
        {
            Action<string> action_ = new Action<string>(test);
            action_ -= test;
            action_ += delegate(string s) { Console.WriteLine("name2:{0}", s); };
            action_("1");
}

测试结果

 

Func 和Action 用法类似,只是其有返回值

说明:Action 和 Func 委托方法,就是一种方法的泛型,可以将方法名作为参数进行传递。

 

posted @ 2017-11-13 11:32  点点_滴滴  阅读(539)  评论(0编辑  收藏  举报