Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 (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; }*/ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

More Books

Students also viewed these Databases questions

Question

Compare levels of resolution in conflict outcomes?

Answered: 1 week ago

Question

Strategies for Managing Conflict Conflict Outcomes?

Answered: 1 week ago