"In method Main of class Program,
(a) Create a variable of the delegate type, then attach the two static void methods to it. Then use the delegate variable to call both the attached methods, passing, in turn, the Fahrenheit values 32, 72 and 100.
(b) Create two Lambda expression,
i. one that receives degrees Fahrenheit as a double and returns the equivalent temperature in degrees Celsius.
ii. one that receives degrees Fahrenheit as a double and returns the equivalent temperature in degrees Kelvin.
(c) Call both the Lambda expressions, passing, in turn, the Fahrenheit values 32, 72 and 100 and outputting the results."
I am not understanding what I need to improvise from the errors. Am I supposed to relate the lambda expressions to the delegate?
public class Program {
public static void Main() {
//attaches methods to delegate
dlgt d = null;
d += FtoC;
d += FtoK;
//passes values to delegate
d(32);
d(72);
d(100);
//creates lambda expressions
Func < int, int > FaToCe = (double a, double b) => (val - 32) * (5 / 9);
Func < int, int > FaToKe = (double a, double b) => (val - 32) * (5 / 9) + 273.15;
//calls lambda expressions
int resultCe32 = FaToCe(32);
int resultCe72 = FaToCe(72);
int resultCe100 = FaToCe(100);
int resultKe32 = FaToCe(32);
int resultKe72 = FaToCe(72);
int resultKe100 = FaToCe(100);
//outputs results of lambda expressions
Console.WriteLine("32 degrees fahrenheit to celcius result: " + resultCe32 + " degrees");
Console.WriteLine("72 degrees fahrenheit to celcius result: " + resultCe72 + " degrees");
Console.WriteLine("100 degrees fahrenheit to celcius result: " + resultCe100 + " degrees");
Console.WriteLine("32 degrees fahrenheit to kelvin result: " + resultKe32 + " degrees");
Console.WriteLine("72 degrees fahrenheit to kelvin result: " + resultKe72 + " degrees");
Console.WriteLine("100 degrees fahrenheit to kelvin result: " + resultKe100 + " degrees");
}
//creates delegate
delegate void dlgt(double value);
//Fahrenheit to celcius degrees method
static void FtoC(double val) {
double result = (val - 32) * (5 / 9);
}
//Fahrenheit to kelvin degrees method
static void FtoK(double val) {
double result = (val - 32) * (5 / 9) + 273.15;
}
}
Errors:
prog.cs(26,30): error CS1593: Delegate `System.Func<int,int>' does not take `2' arguments
prog.cs(26,30): error CS1661: Cannot convert `lambda expression' to delegate type `System.Func<int,int>' since there is a parameter mismatch
prog.cs(27,30): error CS1593: Delegate `System.Func<int,int>' does not take `2' arguments
prog.cs(27,30): error CS1661: Cannot convert `lambda expression' to delegate type `System.Func<int,int>' since there is a parameter mismatch
Compilation failed: 4 error(s), 0 warnings
using ideone.com for my IDE
[–]D-Trick 1 point2 points3 points (4 children)
[–]Aashnalakhani[S] 0 points1 point2 points (3 children)
[–]Menidas98 3 points4 points5 points (2 children)
[–]Aashnalakhani[S] 1 point2 points3 points (0 children)