Example #1
0
        static void Main(string[] args)
        {
            PlayerInfoDel ronaldinho = new PlayerInfoDel(DisplayInformation);

            //delegate     //variable      new deletegate      //method. Thats how this works.
            ronaldinho(); //this calls the first displaymethod.

            PlayerInfoWithNameDel playerName = new PlayerInfoWithNameDel(DisplayInformation);

            playerName("Messi"); //Another way of writting the same line, but separating the variable.

            PlayerInfoNameWithGoalsDel newPlayer = new PlayerInfoNameWithGoalsDel(DisplayInformation);

            newPlayer("Ronaldo", 60); //detects which variable has a string and int and runs the corresponding display method on them.
            newPlayer("Rooney", 25);
            newPlayer("Almeida", 35);


            PlayerBasedOnNumber number = new PlayerBasedOnNumber(DisplayInformation);

            Console.WriteLine(number(8));
            Console.WriteLine(number(10));
            Console.WriteLine(number(7));
        }
Example #2
0
        static void Main(string[] args)
        {
            PlayerInfoDel ronaldinho = new PlayerInfoDel(DisplayInformation);

            ronaldinho();

            PlayerInfoWithNameDel playerName = new PlayerInfoWithNameDel(DisplayInformation);

            playerName("Messi");

            PlayerInfoNameWithGoalsDel newPlayer = new PlayerInfoNameWithGoalsDel(DisplayInformation);

            newPlayer("Ronaldo", 60);
            newPlayer("Rooney", 25);

            PlayerBasedOnNumber number = new PlayerBasedOnNumber(DisplayInformation);

            Console.WriteLine(number(8));
            Console.WriteLine(number(10));

            PlayerInformationWithGoals            playerOne = new PlayerInformationWithGoals(DisplayInformation2);
            PlayerInformationBasedOnNumberAndClub playerTwo = new PlayerInformationBasedOnNumberAndClub(DisplayInformation2);

            //playerOne("Ronaldo", 50);
            //playerTwo(7, "Real Madrid","Portugal");

            Console.WriteLine(playerTwo.Method);

            foreach (var item in playerTwo.Method.GetParameters())
            {
                Console.WriteLine($"{item.ParameterType.Name}, {item.Name}, {item.Position}, {item.IsOptional}, {item.DefaultValue} ");
            }

            SayHiDelegate sayHi = null;

            sayHi  = new SayHiDelegate(SayHiEnglish);
            sayHi += new SayHiDelegate(SayHiSpanish);
            sayHi += new SayHiDelegate(SayHiJapanese);
            sayHi += new SayHiDelegate(SayHiItalian);
            sayHi += new SayHiDelegate(SayHiGerman);
            sayHi += new SayHiDelegate(SayHiArabic);

            sayHi();

            //genreic delegate
            Console.WriteLine("------Generic Delegate------");
            DisplayInfo <int> myNumber = new DisplayInfo <int>(DisplayValue);

            Console.WriteLine(myNumber(42));

            DisplayInfo <double> myDoubleNumber = new DisplayInfo <double>(DisplayValue);

            Console.WriteLine(myDoubleNumber(34.32));

            DisplayInfo <DateTime> myDate = new DisplayInfo <DateTime>(DisplayValue);

            Console.WriteLine(myDate(new DateTime(2010, 2, 23)));

            //Anonymous Method
            Multiply MultiplyNumber = delegate(int n) { { return(n * 3); } };

            Console.WriteLine(MultiplyNumber(10));

            DisplayMessage Message = delegate { Console.WriteLine("Hi from the anonymous method "); };

            Message();

            //Lambda\
            Console.WriteLine("-------Lambda-------");
            MultiplyLambda MultiplyNumberLambda = n => n * 3;

            Console.WriteLine(MultiplyNumber(10));

            DisplayMessageLambda MessageLambda = () => Console.WriteLine("Hi from anonymous lambda");

            Message();

            //Func Delegate
            Console.WriteLine("------Func Delegate-----");
            Func <int, int, int> funcOne = AddTwoNumbers;

            Console.WriteLine(AddTwoNumbers(3, 5));

            Func <int> funcTwo = AddTwoNumbers;

            Console.WriteLine(funcTwo());

            //Action Delegate
            Console.WriteLine("------Action Delegate--------");
            Action <int> actionOne = DisplayInformationActionDelegate;

            actionOne(16);

            Action actionTwo = DisplayInformationActionDelegate;

            actionTwo();

            //Predict Delegate
            Console.WriteLine("-------Predict Delegate------");
            Predicate <int> condition = IsAdmin;

            Console.WriteLine(condition(11));
            Console.WriteLine(condition(10));
        }