Exemple #1
0
        static void Main(string[] args)
        {
            //  create Delegate object from MyDoubleDelegate
            // MyDoubleDelegate d1 = new MyDoubleDelegate(GetAverage);  //  the verbose was or previous version compiler way
            MyDoubleDelegate d1 = GetAverage;      //  a short-cut way to instantiate the instance
            double           arg1 = 2, arg2 = 3;
            double           avg = d1(arg1, arg2); //  compiler issues the .Invoke method on d1

            Console.WriteLine($"The average of { arg1 } and { arg2 } is: { avg }.");


            //  a delegate object referencing an anonymous method (the old way to do it)
            //  conceptually, the dev doesn't need to name every-single-method and why name the method if it's only used
            //      in one place -- by a Delegate
            //  an anonymous method MUST be created already linked to a Delegate so there is a reference (because, its nameless)
            MyDoubleDelegate d2 = delegate(double aa, double bb)
            {
                return(aa * bb);
            };  //  notice the semicolon!

            //  invoke d2 to display the result
            Console.WriteLine($"The product of { arg1 } and { arg2 } is: { d2(arg1, arg2) }.");

            MyDoubleDelegate d3 = delegate(double aa, double bb)
            {
                return(aa / bb);
            };  //  notice the semicolon!

            //  invoke d3 using d3.invoke method
            Console.WriteLine($"The divisor of { arg1 } and { arg2 } is: { d3.Invoke(arg1, arg2) }.");

            //  Lambda expression as the method for the Delegate
            MyDoubleDelegate d4 = (aaa, bbb) => Math.Pow(aaa, bbb);    //  if more than one statement use curly-brackets around the statement list

            Console.WriteLine($"The average of { arg1 } and { arg2 } (as a lambda expression) is: { d4(arg1, arg2) }.");


            //  EXERCISE: Define a Delegate type that takes 2 int and returns a bool
            //              create a delegate object to reference a lambda expression that returns true
            //              if both inputs are even, false otherwise
            int            arg3 = 2, arg4 = 4;
            MyBoolDelegate mbd1 = (xxx, yyy) => (xxx % 2 == 0 && yyy % 2 == 0);

            Console.WriteLine($"The inputs { arg1 } and { arg2 } are EVEN: { mbd1(arg3, arg4) }.");



            Console.WriteLine("\n\nPress <Enter> to exit. . .");
            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            //If we just use the method's name we get the reference
            //of the method. We need to save it in a delegate type
            //that fulfills the signature (return type and argument types)
            //of the method
            MyDoubleDelegate squareNormal1 = Square;

            //The generic delegate Func<Tin, Tout> gives us a
            //really general delegate that can easily be specified
            Func <double, double> squareNormal2 = Square;

            //Now we do the same with a lambda expression
            Func <double, double> squareLambda = x => x * x;

            //Question: What is shorter?

            Console.WriteLine(squareLambda(2));

            //There are other generic types like Action for no return value
            Action <string> printf = str => Console.WriteLine(str);

            //The method behind a delegate can be accessed by using the delegate
            //instance like a method (just calling it with some arguments):
            printf("Hi there!");
            printf("how are you?!");

            //Another popular generic delegate is the predicate, which
            //always returns a boolean
            Predicate <int, int> equal = (l, r) => l == r;

            if (equal(2, 3))
            {
                printf("The two numbers are equal (how?!).");
            }
            else if (equal(3, 3))
            {
                printf("Now that looks more like it!");
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Lambda!");

            //If we just use the method's name we get the reference
            //of the method. We need to save it in a delegate type
            //that fulfills the signature (return type and argument types)
            //of the method
            MyDoubleDelegate squareNormal1 = Square;

            //The generic delegate Func<Tin, Tout> gives us a
            //really general delegate that can easily be specified
            Func <double, double> squareNormal2 = Square;

            //Now we do the same with a lambda expression

            /*
             *
             * A lambda expression is an expression of any of the following two forms:
             *
             * Expression Lambda: Has an expression as its body.
             * (input-parameters) => expression
             *
             * Statement Lambda: Has a statement block as its body.
             * (input-parameters) => { <sequence-of-statements> }
             *
             */
            Func <double, double> squareLambda = x => x * x;

            //Question: What is shorter?

            Console.WriteLine(squareLambda(2));

            //There are other generic types like Action for no return value
            Action <string> printf = str => Console.WriteLine(str);

            //The method behind a delegate can be accessed by using the delegate
            //instance like a method (just calling it with some arguments):
            printf("Hi there!");
            printf("how are you>!");

            //Another popular generic delegate is the predicate, which
            //always returns a boolean

            /*
             *
             * Represents the method that defines a set of criteria and
             * determines whether the specified object meets those criteria.
             * public delegate bool Predicate<in T>(T obj);
             * Read More ...
             *
             */

            //Was unable to do Predicate with two arguments
            //!!!
            int             r     = 10;
            Predicate <int> equal = (l) => l == r;

            l = 2;
            if (equal(l))
            {
                printf("The number is equal");
            }
            else
            {
                printf("The number is not equal (how?!).")

                if (equal(10))
                {
                    printf("The number is equal :)");
                }
            }
        }