Action和Func的简单用法

C#中Action和Func的简单用法
来CSDN的第一天,文章编写还不会,做为一个代码初学者的首次分享,如果有不对的地方希望大家多多原谅和指正。
这串代码是可以顺利运行的,代码如下:

        static void sayhello()
        {
            Console.WriteLine("早上好");
        }
        static void sayhelloOne(int number)
        {
            Console.WriteLine("小明跑了{0}米",number);
        }
        static string TestStr()
        {
            Console.WriteLine("你好");
            return "";
        }
        static double numWord(int num1,float num2)
        {
            Console.WriteLine("这是一个有两个参数的方法,两个数字和为{0}",(float)num1 + num2);
            return 10.0;
        }
        static void Main(string[] args)
        {
            //use "Action" when there is no return value type;   parameter 参数
            Action action = sayhello;
            action();             //no parameter
            Action<int> action01 = sayhelloOne;
            action01(3000);       //have parameter
            //use "Func" when there is no return value type;
            Func<string> func = TestStr;//有一个参数的时候
            func();
            Func<int, float, double> func01 = numWord;//最后一个double为返回值类型
            func01(10,2.2f);
        }


猜你喜欢

转载自blog.csdn.net/weixin_43492764/article/details/83305534