Using delegates and lambda expressions, write code to allow a list of numbers to be aggregated. Show two examples of use: 1) all numbers in the list are summed and 2) all numbers in the list are multiplied?
Example: c# delegate lambda
public class Program
{
public delegate double MathOperation(double x, double y);
static double Addition(double x, double y)
{
return x + y;
}
static double Multiply(double x, double y)
{
return x * y;
}
static double Subtration(double x, double y)
{
return x - y;
}
static void Main(string[] args)
{
MathOperation myFunction = Addition;
Console.WriteLine("The math gives: " + myFunction(2.6, 8.0));
MathOperation myFunction2 = Multiply;
Console.WriteLine("The math gives: " + myFunction2(7.9, 1.6));
MathOperation multiFunction = Addition + Multiply;
multiFunction += Subtration;
multiFunction(3.1, 4.5)
multiFunction -= Addition;
multiFunction(3.1, 4.5)
}
}
public class Program
{
public delegate double MathOperation(double x, double y);
static void Main(string[] args)
{
MathOperation myFunction = delegate(double x, double y) { return x + y; };
Display(myFunction);
}
static void Display(MathOperation myFoo)
{
Console.WriteLine("The math gives: " + myFoo(2.8, 1.5));
}
}
public class Program
{
public delegate double MathOperation(double x, double y);
static void Main(string[] args)
{
MathOperation myFunction = (double x, double y) => { return x + y; };
MathOperation myFunction2 = (x,y) => x * y;
}
}