static void Main(string[] args) { PlayerInfoDelegate messi = new PlayerInfoDelegate(DisplayInformation); messi(); PlayerInfoWithNameDelegate playerName = new PlayerInfoWithNameDelegate(DisplayInformation); playerName("ronaldo"); PlayerInfoWithNameAndGoalsDelegate anotherPlayer = new PlayerInfoWithNameAndGoalsDelegate(DisplayInformation); anotherPlayer("Messi", 60); PlayerBasedOnNumber newPlayer = new PlayerBasedOnNumber(DisplayInformation); Console.WriteLine(newPlayer(10)); PlayerInformationBasedOnNumberAndClub playerOne = new PlayerInformationBasedOnNumberAndClub(DisplayInformation); playerOne(7, "Real Madrid"); // Create a dictionary that will be used in a delegate Dictionary <int, string> listOfItems = new Dictionary <int, string>(); listOfItems.Add(1, "Item One"); listOfItems.Add(2, "Item Two"); listOfItems.Add(3, "Item Three"); listOfItems.Add(4, "Item Four"); listOfItems.Add(5, "Item Five"); DisplayDictionaryContent content = new DisplayDictionaryContent(DisplayDataDictionary); content(listOfItems); }
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)); }
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)); }