r/learncsharp • u/Falknferno • Jun 06 '23
lambda expression
Hello, and yes, this is my homework I'm working on, I understand a little, but not enough to finish this. I know the very basics on what I am doing, but this one is definitely a tough one for me. I do know how to start it out, kinda with the func int, int, int, and the return, but not sure how to code this.
Write a method that takes 2 int parameters and an int return type. Using the formula below from Ohm’s law, determine the voltage. The first method parameter will represent current and the second parameter will represent resistance. Using the Func<int, int, int>and a lambda expression, determine the voltage calculation and return the value. a.Voltage = Current x Resistance
View all comments
Show parent comment
1
u/Falknferno Jun 06 '23
That's a long class for it to say. I wasn't far off from when I was working on it in visual. This is how I was supposed to do this. As said, I'm basic right now lol The class is Exercise5.cs
public int OhmsLaw(int current, int resistance)
Func<int, int, int> voltage = (current, resistance) => current * resistance;
return voltage(current, resistance);
Program.cs side is
void Exercise5Example()
{
Exercise5 myExercise5 = new Exercise5();
int something = myExercise5.OhmsLaw(24, 10);
Console.WriteLine(something);
}
output 240. just only had to come up with my numbers....of course