Exemplo n.º 1
0
        static void Main()
        {
            // * The DoubleApplication higher-order function is called,
            //   passing the Twice function as a parameter
            Console.WriteLine(DoubleApplication(Twice, 3));

            Person[] people = PersonPrintout.CreatePeopleRandomly();
            // * A delegate is passed as a parameter
            Algorithms.SortPeople(people, Person.CompareByAge);
            Console.WriteLine("\nSorted by age:");
            PersonPrintout.ShowPeople(people);

            Algorithms.SortPeople(people, Person.CompareBySurnameAndName);
            Console.WriteLine("\nSorted by surname and name:");
            PersonPrintout.ShowPeople(people);

            // * Delegate variables can be created
            Algorithms.Comparison comparison;
            comparison = Person.CompareByAge;
            Console.WriteLine("\nThe two first people compared by age: {0}",
                              comparison(people[0], people[1]));
            comparison = Person.CompareBySurnameAndName;
            Console.WriteLine("The two first people compared by surname and name: {0}",
                              comparison(people[0], people[1]));
        }
Exemplo n.º 2
0
        static void Main()
        {
            const int a = 2, b = 1;

            // * Lambda expression, with explicit type declaration
            Func <int, int, int> operation = (int p1, int p2) => { return(p1 + p2); };

            Console.WriteLine("Addition : {0}", operation(a, b));
            // * Lambda expression, with explicit type declaration
            // * In case it is a simple return, both the "return" word and { } can be omitted
            operation = (p1, p2) => p1 - p2;
            Console.WriteLine("Subtraction: {0}", operation(a, b));

            // * Let's use Predicate and Action
            // * With one single parameter, () can be omitted
            Predicate <int> isEven         = n => n % 2 == 0;
            Action <string> show;

            if (isEven(a))
            {
                show = (str) => {
                    ConsoleColor color = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(str);
                    Console.ForegroundColor = color;
                }
            }
            ;
            else
            {
                show = Console.WriteLine;
            }
            show("Hello world!");

            // * Higher-order function
            Func <Func <int, int>, int, int> applyTwice = (f, n) => f(f(n));

            Console.WriteLine("Double application of twice: {0}", applyTwice(n => n + n, 3));
            Console.WriteLine("Double of twice in a single statement: {0}",
                              ((Func <Func <int, int>, int, int>)((f, n) => f(f(n))))
                                  (n => n + n, 3)
                              );


            Person[] people = PersonPrintout.CreatePeopleRandomly();
            // * A lambda function is passed as a paramter to a BCL method (FindAll)
            Person[] beyond18 = Array.FindAll(people, person => person.Age >= 18);
            // * The people array supports our ForEach method becase it is an extension method.
            // * A lambda function (action) is passed as a parameter
            beyond18.ForEach(person => Console.WriteLine(person));
        }
Exemplo n.º 3
0
        static void Main()
        {
            const int a = 2, b = 1;
            // * Anonymous delegates (methods)
            Func <int, int, int> binaryOperation        = delegate(int p1, int p2) { return(p1 + p2); };

            Console.WriteLine("Addition: {0}", binaryOperation(a, b));
            binaryOperation = delegate(int p1, int p2) { return(p1 - p2); };
            Console.WriteLine("Subtraction: {0}", binaryOperation(a, b));

            Func <int, int> twice = delegate(int n) { return(n + n); };
            Func <Func <int, int>, int, int> twiceTwice = delegate(Func <int, int> f, int n) { return(f(f(n))); };

            Console.WriteLine("Twice twice: {0}", twiceTwice(twice, 3));

            // In just one statement
            Console.WriteLine("Twice twice in just one statement: {0}",
                              ((Func <Func <int, int>, int, int>)                            // cast
                                   (delegate(Func <int, int> f, int n) { return(f(f(n))); }) // twiceTwice
                              )                                                              // end of cast
                                  (                                                          // the twiceTwice delegate is invoked
                                  delegate(int n) { return(n + n); },                        // first parameter (Funct<int,int>)
                                  3                                                          // second parameter (int)
                                  )                                                          // end of invocation
                              );


            Predicate <int> isEven = delegate(int n) { return(n % 2 == 0); };
            Action <string> show;

            if (isEven(a))
            {
                show = delegate(string str) {
                    ConsoleColor color = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(str);
                    Console.ForegroundColor = color;
                }
            }
            ;
            else
            {
                show = Console.WriteLine;
            }
            show("Hello world!");

            Person[] people   = PersonPrintout.CreatePeopleRandomly();
            Person[] beyond18 = Array.FindAll(people, delegate(Person p) { return(p.Age >= 18); });
            beyond18.ForEach(delegate(Person p) { Console.WriteLine(p); });
        }
Exemplo n.º 4
0
        static void Main()
        {
            const int a = 2, b = 1;

            // * A delegate variable using a user defined type
            BinaryOperation operation     = Program.Add;

            Console.WriteLine("Addition: {0}", operation(a, b));
            operation = Program.Sub;
            Console.WriteLine("Subtraction: {0}", operation(a, b));

            // * A delegate variable using a predefined type
            Func <int, int, int> function = Program.Add;

            Console.WriteLine("Addition: {0}", function(a, b));
            function = Program.Sub;
            Console.WriteLine("Subtaction: {0}", function(a, b));

            // * The DoubleApplication higher-order function is called,
            //   passing another Twice (Func<int,int>) funcion as a parameter
            Console.WriteLine("Double application of twice: {0}", DoubleApplication(Twice, 3));

            // * Two more variables of predefined delegate types
            Predicate <int> isEven        = Program.IsEven;
            Action <string> show;

            if (isEven(a))
            {
                show = Program.ShowInRed;
            }
            else
            {
                show = Console.WriteLine;
            }
            show("Hello world!");

            // * A BCL higher-order method (FindAll) is called,
            //   passing a predicate delegate as a parameter
            Person[] people   = PersonPrintout.CreatePeopleRandomly();
            Person[] beyond18 = Array.FindAll(people, Program.PersonAge18OrBeyond);
            beyond18.ForEach(Console.WriteLine);
        }