Skip to main content

How to Get the Larger of Two Numbers with Math.Max in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<type> <larger> = Math.Max(<a>, <b>);
<value> = Math.Max(<floor>, <value>);
PartWhat it is
Math.MaxThe 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 --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n MathMax
cd MathMax
Output
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:

Program.cs (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:

Program.cs (a score that never drops below 0)
// 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:

Terminal
dotnet run
Output
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

macOS-style terminal window titled Terminal — MathMax showing dotnet run printing Math.Max(7, 12) = 12, Math.Max(-3, -8) = -3, Math.Max(2.5, 2.4) = 2.5, and a score of 3, then 0, then 0 after penalties of 2, 4 and 3

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​

macOS-style code window titled Program.cs — MathMax showing Math.Max called on 7 and 12, on -3 and -8, and on 2.5 and 2.4, and a loop that takes penalties of 2, 4 and 3 from a score of 5 while Math.Max keeps the score at 0 or above

Program.cs
// 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​

MistakeWhat happensDo this instead
Passing three numbersMath.Max(4, 9, 6) didn't build: error CS1501: No overload for method 'Max' takes 3 argumentsNest 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 intint 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 doubleKeep it in a double: double larger = Math.Max(3, 2.5); printed 3
Using Math.Max to cap a numberTo keep a volume at 100 or lower, Math.Max(100, volume) turned 40 into 100 and let 120 through as 120A 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:

Program.cs (Math.Clamp)
int[] values = [-5, 42, 120];
foreach (int value in values)
{
Console.WriteLine($"Math.Clamp({value}, 0, 100) = {Math.Clamp(value, 0, 100)}");
}
Output
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:

Program.cs (NaN)
Console.WriteLine($"Math.Max(double.NaN, 5.0) = {Math.Max(double.NaN, 5.0)}");
Output
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​