Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The lambda expression: (o1, o2) => o1 + o2 is used to add 2 double values. Would

ID: 3879888 • Letter: T

Question

The lambda expression: (o1, o2) => o1 + o2 is used to add 2 double values. Would this lambda expression work to add 2 integer values?

Assume it is being passed to this generic delegate: Func<int, int, int> (see code below).

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment1c
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter first operand: ");
            double num1 = double.Parse(Console.ReadLine());

            Console.Write("Enter second operand: ");
            double num2 = double.Parse(Console.ReadLine());
          
            GenericMath genMath = new GenericMath() { Operand1 = num1, Operand2 = num2 };

            double sum = genMath.DoMathProblem((o1, o2) => o1 + o2);
            Console.WriteLine($"The sum is {sum}");

            double diff = genMath.DoMathProblem((o1, o2) => o1 - o2);
            Console.WriteLine($"The difference is {diff}");

            double mult = genMath.DoMathProblem((o1, o2) => o1 * o2);
            Console.WriteLine($"The product is {mult}");

            double div = genMath.DoMathProblem((o1, o2) =>
            {
                if (o2 != 0)
                    return o1 / o2;
                else
                {
                    throw new DivideByZeroException();
                }
            });
            Console.WriteLine($"The quotient is {div}");

#if DEBUG
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
#endif
        }

        /*static double Add(double operand1, double operand2)
        {
            return operand1 + operand2;
        }*/

        /*static double Subtract(double operand1, double operand2)
        {
            return operand1 - operand2;
        }*/
    }
}

Explanation / Answer

Hi,

Yes the lamba expression (o1, o2) => o1 + o2 will work and gives us the result of addition of two numbers as we have defined the Add function.

No, the argument "int, int, int" will not work as a parameter for  genMath.DoMathProblem method. This is because we have defined only two operand namely operand 1 and operand 2 while creating the object for GenericMath class. And we have defined 4 operations on these two operands namely addition, multiplication, subtraction and division.

All of these operations can be applied to two operands as per current code. But, the given scenario, we are trying to pass 3 operands of integer types which is invalid and will result and error.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote