Answer to Question #189418 in C# for CHANDRASENA REDDY

Question #189418

Q2. Do a peer review of this code and suggest if any improvements can be done.


Task : Mahesh has suggested that we should define one function called PerformArithmeticOperation( ) which will take 2 numbers and the Delegate as parameters. Instead of calling / executing the delegate from Main() function, let this function manage the delegate call. You have to implement this functionality.


Hint: The PerformArithmeticOperation() function signature is:

static void PerformArithmeticOperation( int num1, int num2, MyDelegate arOperation){…}


1
Expert's answer
2021-05-15T07:32:24-0400
using System;


namespace Q2
{
    class Program
    {
        delegate int MyDelegate(int num1, int num2);


        static void Main(string[] args)
        {
            MyDelegate arOperation = (int num1, int num2) => num1 + num2;
            PerformArithmeticOperation(1, 2, arOperation);
        }
                
        static void PerformArithmeticOperation(int num1, int num2, MyDelegate arOperation)
        {
            var result = arOperation(num1, num2);
            Console.WriteLine(result);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS