委托、匿名方法和 Lambda 表达式

/// <summary>
    /// ProgramName:委托、匿名方法和 Lambda 表达式
    /// </summary>
    class Program
    {

        //C#1.0  Delegate Declare
        delegate string MyTestC1(string str);

        //C#2.0  Delegate Declare
        delegate string MyTestC2(string str);

        //C#3.0  Delegate Declare
        delegate string MyTestC3(string str);

        static void Main(string[] args)
        {
            Program classP=new Program();
            classP.C1Function();
            classP.C2Function();
            classP.C3Function();
        }

        //C#1.0  Delegate Function
        string MyFunction(string str)
        {
            return "C#1.0 -> " + str;
        }

        //C#1.0  Delegate Transfer
        void C1Function()
        {
            Program classP = new Program();
            MyTestC1 c1 = classP.MyFunction;
            Console.WriteLine(c1("Hello Word!"));
        }

        void C2Function()
        {
            //C#2.0  Delegate Function
            MyTestC2 c2 = delegate(string str)
            {
                return "C#2.0 -> " + str;
            };

            //C#2.0  Delegate Transfer
            Console.WriteLine(c2("Hello Word!"));
        }

        void C3Function()
        {
            //C#3.0  Delegate Function
            MyTestC3 c3 = ((str) => "C#3.0 -> " + str);
            //C#3.0  Delegate Transfer
            Console.WriteLine(c3("Hello Word!"));
        }

    }

你可能感兴趣的:(lambda)