首页 > 其他 > 详细

委托的N种写法,你喜欢哪种?

时间:2014-01-14 21:42:08      阅读:465      评论:0      收藏:0      [点我收藏+]

一、委托调用方式

1. 最原始版本:

bubuko.com,布布扣
    delegate string PlusStringHandle(string x, string y);
    class Program
    {
        static void Main(string[] args)
        {
            PlusStringHandle pHandle = new PlusStringHandle(plusString);
            Console.WriteLine(pHandle("abc", "edf"));

            Console.Read();
        }

        static string plusString(string x, string y)
        {
            return x + y;
        }
    }
bubuko.com,布布扣

 2. 原始匿名函数版:去掉“plusString”方法,改为

bubuko.com,布布扣
            PlusStringHandle pHandle = new PlusStringHandle(delegate(string x, string y)
            {
                return x + y;
            });
            Console.WriteLine(pHandle("abc", "edf"));
bubuko.com,布布扣

3. 使用Lambda(C#3.0+),继续去掉“plusString”方法(以下代码均不再需要该方法)

bubuko.com,布布扣
            PlusStringHandle pHandle = (string x, string y) =>
            {
                return x + y;
            };
            Console.WriteLine(pHandle("abc", "edf"));
bubuko.com,布布扣

还有更甚的写法(省去参数类型)

bubuko.com,布布扣
            PlusStringHandle pHandle = (x, y) =>
            {
                return x + y;
            };
            Console.WriteLine(pHandle("abc", "edf"));
bubuko.com,布布扣

如果只有一个参数

bubuko.com,布布扣
        delegate void WriteStringHandle(string str);
        static void Main(string[] args)
        {
            //如果只有一个参数
            WriteStringHandle handle = p => Console.WriteLine(p);
            handle("lisi");

            Console.Read();
        }
bubuko.com,布布扣

 

二、委托声明方式

1. 原始声明方式见上述Demo

2. 直接使用.NET Framework定义好的泛型委托 Func 与 Action ,从而省却每次都进行的委托声明。

bubuko.com,布布扣
        static void Main(string[] args)
        {
            WritePrint<int>(p => Console.WriteLine("{0}是一个整数", p), 10);

            Console.Read();
        }

        static void WritePrint<T>(Action<T> action, T t)
        {
            Console.WriteLine("类型为:{0},值为:{1}", t.GetType(), t);
            action(t);
        }
bubuko.com,布布扣

3. 再加上个扩展方法,就能搞成所谓的“链式编程”啦。

bubuko.com,布布扣
    class Program
    {   
        static void Main(string[] args)
        {
            string str = "所有童鞋:".plusString(p => p = p + " girl: lisi、lili\r\n").plusString(p => p + "boy: wangwu") ;
            Console.WriteLine(str);

            Console.Read();
        }
    }

    static class Extentions
    {
        public static string plusString<TParam>(this TParam source, Func<TParam, string> func)
        {
            Console.WriteLine("字符串相加前原值为:{0}。。。。。。", source);
            return func(source);
        }
    }
bubuko.com,布布扣

看这个代码是不是和我们平时写的"list.Where(p => p.Age > 18)"很像呢?没错Where等方法就是使用类似的方式来实现的。

好了,我总结完了,如有遗漏,还望补上,臣不尽感激。

委托的N种写法,你喜欢哪种?

原文:http://www.cnblogs.com/xiaofengfeng/p/3513681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!