❔ Finding max from the list using lambda expression

Hello, I have an example of a following list, something like {1, 2, 3, 4, 5, 6} I want to find the max element of this list using lambda expression. So this is what I coded
List<int> sample;
int max = sample.Max(e => e);
List<int> sample;
int max = sample.Max(e => e);
Do we have to fill in the expression in List.Max? Is there other way to do it? Thank you.
3 Replies
TheBoxyBear
TheBoxyBear2y ago
In .NET 7 at least there's an overload that doesn't take a selector Actually present as far back as .NET 5 and Framework 4.8.1 but it obviously requires the collection to be a numeric type which it is in your case. When typing the method name, you can click the arrows to see the overloads
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
new[] {1,8,4,6,2,7}.Max()
new[] {1,8,4,6,2,7}.Max()
Result: int
8
8
Compile: 438.641ms | Execution: 44.249ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server