static void Main(string[] args) { Multiplayer calc = (x, y) => x * y; //same as Func<int,int,int> PrintSomething action = () => Console.WriteLine("Hello!"); //same as action() Action <string> print = message => Console.WriteLine(message); }
delegate void PrintSomething(object x); //pass anything static void Main(string[] args) { //how to call anonymous method: //imagine that you are declaring a variable as method ????????imagine?????? //syntax: anonymoudMethod m1 = delegate { }; anonymoudMethod m1 = delegate { }; // as type looooooooooooooool m1(); //-> method name now called m1, anonymoudMethod m2 = delegate { string name = "Salah"; Console.WriteLine(name); }; //Create string m2(); //-> method name called m2() anonymoudMethod m3 = delegate { Console.WriteLine("Anonymous Method - 3"); }; //add things m3(); //-> method name called m3() anonymoudMethod m4 = delegate { //here is method, returns void - do whatever with it Console.WriteLine("Welcome to anonymous method#4"); }; m4(); Console.WriteLine("===================================="); //now call another anonymous method that has a return type int: returnIntNow rInt_1 = delegate { return(1); }; //must return something - return int type Console.WriteLine(rInt_1()); //1 Console.WriteLine("===================================="); returnIntNow rInt_2 = delegate { int x = 15, y = 16, res = x * y; return(res); }; //must return int type Console.WriteLine(rInt_2()); //15*16=240 Console.WriteLine("===================================="); //use PrintSomething(object x) PrintSomething Ps = delegate(object x) { Console.WriteLine("Object passed => {0}", x); }; Ps("salah"); Ps("Imad"); Ps("Bedeiwi"); Ps(153.65); Ps(18); Console.WriteLine("===================================="); PrintSomething getNumTimes = delegate(object x) { int square = (int)x * (int)x; Console.WriteLine("Square of {0} is {1}", x, square); }; getNumTimes(15); //125 Console.WriteLine("===================================="); //get square of all values in array int[] arr1 = new int[] { 15, 18, 19, 20, 21, 22, 23, 36 }; foreach (int a in arr1) { getNumTimes(a); } Console.ReadKey(); }
public void InvokeDelegate() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var myDelegate = new PrintSomething(PrintTime); while (true) { if (stopwatch.Elapsed.Seconds != this.seconds) { continue; } myDelegate.Invoke(); stopwatch.Restart(); } }
static void Main() { //Problem 1 test string text = "Some short text"; Console.WriteLine(text.SBSubstring(2, 6) + "\n"); //Problem 2 test int[] numbers = { 2, 4, 6, 7, 3 }; double[] doubleNumbers = { 2.4, 5.6, 14.2, 23.345 }; Console.WriteLine(numbers.SumElements()); Console.WriteLine(numbers.MaxElement()); Console.WriteLine(numbers.MinElement()); Console.WriteLine(numbers.ProductOfElements()); Console.WriteLine(numbers.Avg() + "\n"); Console.WriteLine(doubleNumbers.SumElements()); Console.WriteLine(doubleNumbers.MaxElement()); Console.WriteLine(doubleNumbers.MinElement()); Console.WriteLine(doubleNumbers.ProductOfElements()); Console.WriteLine(doubleNumbers.Avg() + "\n"); //Problem 3 test Students ivan = new Students("Ivan", "Petrov", 15); Students anotherIvan = new Students("Ivan", "Dimitrov", 17); Students petar = new Students("Petar", "Ivanov", 20); Students mariya = new Students("Mariya", "Dimitrova", 25); Students petya = new Students("Petya", "Angelova", 22); Students dimitar = new Students("Dimitar", "Kostov", 19); Students[] students = { ivan, anotherIvan, petar, mariya, petya, dimitar }; Console.WriteLine("All students:\n" + string.Join(", ", students) + "\n"); Console.WriteLine("Students, whose first name is before their last name alphabeticaly:\n" + LINQ.FirstBeforeLastName(students) + "\n"); //Problem 4 test Console.WriteLine("Students who are between 18 and 24 years old:\n" + LINQ.AgeRange(students) + "\n"); //Problem 5 test Console.WriteLine("Decsending ordered Students:\n" + LINQ.OrderStudents(students) + "\n"); //Problem 6 test int[] nums = { 2, 4, 5, 7, 9, 14, 27 }; Console.WriteLine("Numbers divisible by 7 and 3:\n" + LINQ.DivisibleBy7And3(nums) + "\n"); //Problem 7 test - Timer: prints only 5 times so it takes 10seconds Console.WriteLine("Timer:"); int counter = 0; while (counter < 5) { Thread.Sleep(2000); PrintSomething print = Timer.PrintTime; print(2); counter++; } //Problem 9 test Student ivanIvanov = new Student("Ivan", "Ivanov", 123006, "+359 123123123", "*****@*****.**", new List <int> { 2, 3, 5, 4, 5, 2 }, 2); Student petyaPetrova = new Student("Petya", "Petrova", 258006, "+359 258258258", "*****@*****.**", new List <int> { 4, 2, 2, 5, 6, 2 }, 3); Student dimitarKolev = new Student("Dimitar", "Kolev", 369008, "02/ 369369369", "*****@*****.**", new List <int> { 5, 6, 3, 6, 4, 2 }, 2); List <Student> anotherStudents = new List <Student> { ivanIvanov, petyaPetrova, dimitarKolev }; Console.WriteLine("All students:\n" + string.Join("\r\n", anotherStudents) + "\n"); Console.WriteLine("Students by group 2:\n" + LINQ.StudentsByGroup2(anotherStudents)); //Problem 10 test - the same as problem 9 with extension method Console.WriteLine(anotherStudents.ByGroup2()); //Problem 11 test Console.WriteLine("Students with e-mail in abv.bg:\n" + LINQ.StudentsAbv(anotherStudents) + "\n"); //Problem 12 test - Sofia's phone code is 02 Console.WriteLine("Students with telephones from Sofia:\n" + LINQ.PhonesFromSofia(anotherStudents) + "\n"); //Problem 13 test Console.WriteLine("Students who have least one exellent marks:\n" + LINQ.OneOrMoreExellentMarks(anotherStudents) + "\n"); //Problem 14 test Console.WriteLine("Students who have exactly two poor marks:\n" + string.Join("\r\n", anotherStudents.TwoPoorMarks()) + "\n"); //Problem 15 test - 5th and 6th digits are 0 and 6 Console.WriteLine("Students that enrolled in 2006:\n" + string.Join("\r\n", anotherStudents.StudentsIn2006()) + "\n"); //Problem 17 test string[] fruits = { "apple", "orange", "banana", "strawberry" }; Console.WriteLine("Longest string: " + LINQ.LongestString(fruits) + "\n"); //Problem 18 test LINQ.GroupByGroup(anotherStudents); //Problem 19 test anotherStudents.GroupStudents(); }
public static void SendString(PrintSomething ps) { //add code that invokes delegate object ps("Total awesomeness that you will never forget"); }