fanc委托在项目中使用

一,上代码

using System;

namespace FuncDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //无参数的fanc委托
            var ss1 = FuncCommon.GetT<string>(() => {

               return "HelloWorld";

            });
            Console.WriteLine(ss1);

            //带参数的fanc委托
            var ss = FuncCommon.GetTn<string>((str,m) =>
            {
                var cc = str + DateTime.Now.AddDays(1).ToString();
                return cc+ m;
            });
            Console.WriteLine(ss);
            Console.ReadKey();
        }
    }
}

 

二,类代码

using System;
using System.Collections.Generic;
using System.Text;

namespace FuncDemo
{
    public static class FuncCommon
    {
        public static T GetT<T>(Func<T> func)
        {
            T t = default(T);
            t = func.Invoke();
            return t;
        }

        public static T GetTn<T>(Func<string,string, T> func)
        {
            T t = default(T);
            t = func.Invoke("HelloWorld","\n"+DateTime.Now.ToString());
            return t;
        }
    }
}

三,结果

 

posted @ 2019-05-05 14:32  叶丶梓轩  阅读(276)  评论(0编辑  收藏  举报