How to Find the Largest Value with LINQ Max in C#
What's the highest score? How long is the longest word? In C#, LINQ's Max answers both: on its own it returns the largest value in a collection, and with a small function it returns the largest value it works out from each item.
Syntax
<Type> <largest> = <items>.Max();
<Type> <largest> = <items>.Max(<item> => <value from item>);
| Part | What it does |
|---|---|
<items>.Max() | The largest value in the collection, such as the highest number in an int[] |
<items>.Max(<item> => <value from item>) | Works out a value from each item, and returns the largest of those values |
Microsoft's Enumerable.Max reference describes the first form for whole numbers: "Returns the maximum value in a sequence of Int32 values." And the second: "Invokes a transform function on each element of a sequence and returns the maximum Int32 value."
The Example
The program finds the highest of three test scores, then the length of the longest word in a short list of fruit.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

int[] scores = [72, 95, 88];
Console.WriteLine(scores.Max());
string[] words = ["fig", "banana", "kiwi"];
Console.WriteLine(words.Max(word => word.Length));
word => word.Length is a lambda: for each word, it gives its Length.
Step 2: Run It
dotnet run main.cs
95
6

- 95 is the highest of the three scores.
- The lengths were 3, 6 and 4, so
Maxreturned 6: the length, not the word.
The command is the same in zsh, bash and PowerShell.
An Empty Collection Throws
With nothing to compare, Max has no answer:
int[] scores = [];
Console.WriteLine(scores.Max());
Unhandled exception. System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.MinMaxInteger[T,TMinMax](IEnumerable`1 source)
at Program.<Main>$(String[] args) in /Users/you/demo/main.cs:line 2
When a collection can be empty, check first and choose what "no scores" means:
int[] scores = [];
Console.WriteLine(scores.Length > 0 ? scores.Max() : 0);
0
The Item, Not the Value: MaxBy
To get the longest word itself rather than its length, use MaxBy with the same lambda:
string[] words = ["fig", "banana", "kiwi"];
Console.WriteLine(words.MaxBy(word => word.Length));
banana
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling Max() on a collection that can be empty | InvalidOperationException: Sequence contains no elements | Check Length or Count first |
Expecting Max(word => word.Length) to return the word | It returns the largest length, 6 | Use MaxBy for the item |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Sequence contains no elements | Max ran on an empty collection | Handle the empty case before calling it |
| A number came back where you expected an item | Max with a lambda returns the value | Switch to MaxBy |
FAQ
Does Max need a using directive?
Not here: Max is a LINQ method from System.Linq, and a file run with dotnet run includes that namespace by default, so the example has no using line.
Is there a Min too?
Yes. Min works the same way for the smallest value, and MinBy for the item: on the same data, scores.Min() gave 72, and words.MinBy(word => word.Length) gave fig.
Conclusion
items.Max() returns the largest value, items.Max(item => value) the largest value worked out from each item, and items.MaxBy(item => value) the item that has it. On an empty collection, Max throws, so check first when it can be empty.
Additional Resources
- Enumerable.Max method, on Microsoft Learn.
- Enumerable.MaxBy method, on Microsoft Learn.
