Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class Program
  2. {
  3. static void Main()
  4. {
  5. // You can also pass in any Function() that matches the delegate signature
  6. MyMainMethod(5, 3, AddTwoNumbers);
  7. MyMainMethod(5, 3, SubtractTwoNumbers);
  8. MyMainMethod(5, 3, MultiplyTwoNumbers);
  9. MyMainMethod(5, 3, DivideTwoNumber);
  10. }
  11.  
  12. private static void MyMainMethod(double number1, double number2, Func<double, double, double> funcToCall)
  13. {
  14. Console.WriteLine("Startinging computation");
  15. var totalTime = 0.0;
  16. var watch = new Stopwatch();
  17. watch.Start();
  18.  
  19. // This is where delegate rock!! Think a need
  20. // to call duplicate code with thousands of lines
  21. var result = funcToCall(number1, number2);
  22.  
  23. watch.Stop();
  24. totalTime = watch.ElapsedMilliseconds;
  25. Console.WriteLine($"It took {totalTime}, milliseconds to compute result: {result}.");
  26. }
  27.  
  28. static double AddTwoNumbers(double a, double b) { return a + b; }
  29. static double SubtractTwoNumbers(double a, double b) { return a - b; }
  30. static double MultiplyTwoNumbers(double a, double b) { return a * b; }
  31. static double DivideTwoNumber(double a, double b) { return a / b; }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement