How to Get the Larger of Two Numbers with Math.Max in C#
Math.Max(a, b) returns the larger of two numbers. It's also the shortest way to keep a value from dropping below a floor, such as a score that never goes under 0.
Microsoft's reference for Math.Max: Returns the larger of two specified numbers. Math is a static class in the System namespace, so you call Max on the class itself, without creating anything. Each version of Max takes two numbers of the same type and returns that type. For two int values, it returns, in the reference's words, Parameter val1 or val2, whichever is larger. val1 and val2 are the two numbers you pass.
Syntax
<type> <larger> = Math.Max(<a>, <b>);
<value> = Math.Max(<floor>, <value>);
| Part | What it is |
|---|---|
Math.Max | The static method of the Math class that returns the larger of two numbers |
<a>, <b> | The two numbers to compare, such as two int values or two double values |
<type> <larger> | The result has the type of the two numbers: an int for two int values |
<value> = Math.Max(<floor>, <value>); | A floor: after this line, <value> is never lower than <floor> |
The Example
A console app called MathMax compares three pairs of numbers: two positive, two negative and two double values. Then it takes three penalties from a game score and uses Math.Max to keep the score from dropping below 0.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n MathMax
cd MathMax
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/MathMax/MathMax.csproj:
Determining projects to restore...
Restored /Users/you/source/MathMax/MathMax.csproj (in 41 ms).
Restore succeeded.
dotnet new console -n MathMax creates a folder named MathMax with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or MathMax.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First, three comparisons:
// Math.Max returns the larger of two numbers.
Console.WriteLine($"Math.Max(7, 12) = {Math.Max(7, 12)}");
Console.WriteLine($"Math.Max(-3, -8) = {Math.Max(-3, -8)}");
Console.WriteLine($"Math.Max(2.5, 2.4) = {Math.Max(2.5, 2.4)}");
Each call gets two numbers and returns the larger one. -3 and -8 are negative, and 2.5 and 2.4 are double values.
Then the floor:
// A floor: a game score that never drops below 0.
int score = 5;
int[] penalties = [2, 4, 3];
foreach (int penalty in penalties)
{
score = Math.Max(0, score - penalty);
Console.WriteLine($"Penalty {penalty}: score is {score}");
}
The score starts at 5, and each penalty takes points away. score - penalty can go below 0, but then 0 is the larger of the two, so Math.Max(0, score - penalty) returns 0.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Math.Max(7, 12) = 12
Math.Max(-3, -8) = -3
Math.Max(2.5, 2.4) = 2.5
Penalty 2: score is 3
Penalty 4: score is 0
Penalty 3: score is 0

Math.Max(7, 12) returned 12, and Math.Max(-3, -8) returned -3: of two negative numbers, the larger is the one closer to zero. For the two double values it returned 2.5. The score went from 5 to 3. The penalty of 4 would have taken it to -1, so Math.Max returned 0, and after the last penalty it stayed at 0.
The Complete File

// Math.Max returns the larger of two numbers.
Console.WriteLine($"Math.Max(7, 12) = {Math.Max(7, 12)}");
Console.WriteLine($"Math.Max(-3, -8) = {Math.Max(-3, -8)}");
Console.WriteLine($"Math.Max(2.5, 2.4) = {Math.Max(2.5, 2.4)}");
// A floor: a game score that never drops below 0.
int score = 5;
int[] penalties = [2, 4, 3];
foreach (int penalty in penalties)
{
score = Math.Max(0, score - penalty);
Console.WriteLine($"Penalty {penalty}: score is {score}");
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Passing three numbers | Math.Max(4, 9, 6) didn't build: error CS1501: No overload for method 'Max' takes 3 arguments | Nest the calls: Math.Max(4, Math.Max(9, 6)) returned 9. For an array, LINQ's numbers.Max() also returned 9 |
Keeping the result of an int and a double in an int | int larger = Math.Max(3, 2.5); didn't build: error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). With one double in the pair, the result is a double | Keep it in a double: double larger = Math.Max(3, 2.5); printed 3 |
Using Math.Max to cap a number | To keep a volume at 100 or lower, Math.Max(100, volume) turned 40 into 100 and let 120 through as 120 | A cap needs the smaller number: Math.Min(100, volume) gave 40 and 100 |
FAQ
How do I keep a number between a lowest and a highest value?
Use Math.Clamp, which is a floor and a cap in one call:
int[] values = [-5, 42, 120];
foreach (int value in values)
{
Console.WriteLine($"Math.Clamp({value}, 0, 100) = {Math.Clamp(value, 0, 100)}");
}
Math.Clamp(-5, 0, 100) = 0
Math.Clamp(42, 0, 100) = 42
Math.Clamp(120, 0, 100) = 100
Math.Clamp(value, 0, 100) takes the value, then the lowest and the highest value allowed. It took -5 up to 0, kept 42, and brought 120 down to 100. The reference: Returns value clamped to the inclusive range of min and max.
What does Math.Max return when one of the numbers is NaN?
NaN, the "not a number" value of double:
Console.WriteLine($"Math.Max(double.NaN, 5.0) = {Math.Max(double.NaN, 5.0)}");
Math.Max(double.NaN, 5.0) = NaN
The reference for the double version: Parameter val1 or val2, whichever is larger. If val1, val2, or both val1 and val2 are equal to NaN, NaN is returned.
Conclusion
Math.Max(a, b) returns the larger of two numbers of the same type. Write value = Math.Max(floor, value); to keep a value from dropping below a floor, nest calls for more than two numbers, and use Math.Min for a cap or Math.Clamp for both.
Additional Resources
Math.MaxMethod — every overload, and how thedoubleversion treats NaN.Math.MinMethod — the smaller of two numbers, for a cap.Math.ClampMethod — keeps a value between a lowest and a highest value.Enumerable.MaxMethod — the largest value of a whole collection.
