懵懂的委托

利用委托和lambda表达式可以封装自定义的方法,所实话,我能写这个代码,但是原理我有点说不大清,先记下来

 

class Program
    {


        static void Main()
        {

            Console.WriteLine(Command<string>(r =>
            {
                if (r == 1)
                    return "你大爷";
                else
                    return "zade";
            }));
            Console.WriteLine(Command<string>(null));
            Console.ReadKey();
        }



        public static T Command<T>(Func<int, T> process)
        {
            if (process == null)
            {
                return default(T);
            }
            if (DateTime.Now.Second % 2 == 0)
            {
                return process(2);
            }
            else
            {
                return process(1);
            }
        }

    }

 

posted on 2017-06-19 16:23  奔游浪子  阅读(64)  评论(0)    收藏  举报

导航