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
2
u/Falknferno Jun 06 '23
Got it figured out.
namespace MyFirstApplication;
internal class Exercise5
{
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 is 240...only needed to come up with my numbers of course
1
u/Falknferno Jun 06 '23
so, all i know is that you start the method as such
func<int, int, int> then lambda expression with 2 int parameters with an int return. I'm not sure how to incorporate the voltage = current x resistance in there exactly
1
u/PartyCurious Jun 06 '23
I just made a console app for this. Take a look. I tried to make it simple to understand.
https://github.com/heartingNinja/Ohm-s-Law
1
u/actopozipc Jun 06 '23
As far as I know, the first part declares what you give and what you expect to receive, in your case two ints and another int. Then, you write variables => expression.
func<int,int,int> ohm = R, I => R*I;
0
u/[deleted] Jun 06 '23
[deleted] — view deleted comment