Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main()
- {
- // You can also pass in any Function() that matches the delegate signature
- MyMainMethod(5, 3, AddTwoNumbers);
- MyMainMethod(5, 3, SubtractTwoNumbers);
- MyMainMethod(5, 3, MultiplyTwoNumbers);
- MyMainMethod(5, 3, DivideTwoNumber);
- }
- private static void MyMainMethod(double number1, double number2, Func<double, double, double> funcToCall)
- {
- Console.WriteLine("Startinging computation");
- var totalTime = 0.0;
- var watch = new Stopwatch();
- watch.Start();
- // This is where delegate rock!! Think a need
- // to call duplicate code with thousands of lines
- var result = funcToCall(number1, number2);
- watch.Stop();
- totalTime = watch.ElapsedMilliseconds;
- Console.WriteLine($"It took {totalTime}, milliseconds to compute result: {result}.");
- }
- static double AddTwoNumbers(double a, double b) { return a + b; }
- static double SubtractTwoNumbers(double a, double b) { return a - b; }
- static double MultiplyTwoNumbers(double a, double b) { return a * b; }
- static double DivideTwoNumber(double a, double b) { return a / b; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement