static void Main(string[] args)
        {
            MyFirstDelegate forward = delegate(string s2)
            {
                Console.WriteLine("This is my string: {0}", s2);
            };

            forward("No Red October this year!");
        }
示例#2
0
        static void Main(string[] args)
        {
            MyFirstDelegate f1 = new MyFirstDelegate(Print);

            f1("Sagar");
            f1 += new MyFirstDelegate(SayHello);
            f1("Hiiiiiiiiiiii");
            Console.Read();
        }
示例#3
0
        static void Main(string[] args)
        {
            MyFirstDelegate myFirstDelegate = new MyFirstDelegate(LambdaExpressions.WriteToConsoleForward);

            myFirstDelegate("I am a Software Engineer!");

            myFirstDelegate = new MyFirstDelegate(LambdaExpressions.WriteToConsoleBackward);

            myFirstDelegate("I am a Software Engineer!");
        }
        static void Main(string[] args)
        {
            MyFirstDelegate fd1 = Fun1;
            MyFirstDelegate fd2 = Fun2;
            MyFirstDelegate fd3 = Fun3;

            //MyFirstDelegate fd4 = fd1 + fd2 + fd3;

            fd1();
            fd2();
            fd3();
            //fd4();

            Console.ReadKey();
        }
示例#5
0
 static void WriteToConsole(MyFirstDelegate myDelegate, string stringToWrite)
 {
     myDelegate(stringToWrite);
 }
示例#6
0
        static void Main(string[] args)
        {
            square myLambdaExpression = x => x * x;

            Console.WriteLine("X squared is {0}", myLambdaExpression(5));

            GreaterThan gt = (x, y) => x > y;

            Console.WriteLine("Is 6 greater than 5. {0}", gt(6, 5));

            string myLocalString = "Hello World";

            //Call the function the normal way.
            WriteToConsoleForward(myLocalString);
            WriteToConsoleBackwards(myLocalString);

            //Call the function using a delegate.
            MyFirstDelegate myFirstDelegate = new MyFirstDelegate(LambdaExpressionExample.WriteToConsoleForward);

            myFirstDelegate(myLocalString);

            //Call the fuction by passing a reference to the function.
            WriteToConsole(LambdaExpressionExample.WriteToConsoleForward, myLocalString);
            WriteToConsole(LambdaExpressionExample.WriteToConsoleBackwards, myLocalString);

            //Create an anonymous method using the local variable.
            MyAnonymousMethod forward = delegate()
            {
                Console.WriteLine(string.Format("This is my string: {0}", myLocalString));
            };

            forward();

            //Create an anonymous method with the original delegate.
            MyFirstDelegate backward = delegate(string s2)
            {
                char[] charArray = s2.ToCharArray();
                Array.Reverse(charArray);
                Console.WriteLine(string.Format("This is my string backwards: {0}", new string(charArray)));
            };

            backward(myLocalString);

            //Use a Lambda expression statement with a local variable.
            MyAnonymousMethod myFirstDelegate2 = () =>
            {
                char[] charArray = myLocalString.ToCharArray();
                Array.Reverse(charArray);
                Console.WriteLine(string.Format("This is my string backwards: {0}", new string(charArray)));
            };

            myFirstDelegate2();

            //Use a Lambda expression statement with a parameter.
            MyFirstDelegate myFirstDelegate3 = s =>
            {
                char[] charArray = s.ToCharArray();
                Array.Reverse(charArray);
                Console.WriteLine(string.Format("This is my string backwards: {0}", new string(charArray)));
            };

            myFirstDelegate3(myLocalString);

            WriteToConsole(x => Console.WriteLine("This is my string {0}", x), "Hello World");

            Console.ReadLine();
        }
示例#7
0
        private void button3_Click(object sender, EventArgs e)
        {
            MyFirstDelegate myFailDelegate = new MyFirstDelegate(callMeOnFail);

            myFailDelegate.Invoke();
        }
示例#8
0
        private void button1_Click(object sender, EventArgs e)
        {
            MyFirstDelegate mySuccessDelegate = new MyFirstDelegate(callMeOnSuccess);

            mySuccessDelegate.Invoke();
        }