匿名委托与λ表达式

    delegate string DelegateTest(string val);
        delegate string DelegateTest2(string val,int val2);
        static void Main(string[] args)
        {
            string mid = ",middle part,";

            DelegateTest anonDel = delegate(string param)
            {
                param += mid;
                param += "and this was added to the string.";
                return param;
            };

            DelegateTest2 anonDel2 = (param,param2) =>
                {
                    param += mid;
                    param += "and this was added to the string.";
                    param += param2.ToString();
                    return param;
                };

            DelegateTest anonDel3 = param => param += "and this was added to the string.";

            Console.WriteLine(anonDel("Start of string"));
            Console.WriteLine(anonDel2("Start of string",2));
            Console.WriteLine(anonDel3("Start of string"));
            Console.Read();
        }

posted on 2010-02-23 17:59  态度决定一切  阅读(98)  评论(0)    收藏  举报

导航