How to Get the Smaller of Two Numbers with Math.Min in C#
Math.Min(a, b) returns the smaller of two numbers. It's also the shortest way to put a ceiling on a value, such as never selling more than you have in stock.
Microsoft's reference for Math.Min: Returns the smaller of two numbers. Math is a static class in the System namespace, so you call Min on the class itself, without creating anything. Each version of Min 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 smaller. val1 and val2 are the two numbers you pass.
Syntax
<type> <smaller> = Math.Min(<a>, <b>);
<value> = Math.Min(<value>, <ceiling>);
| Part | What it is |
|---|---|
Math.Min | The static method of the Math class that returns the smaller of two numbers |
<a>, <b> | The two numbers to compare, such as two int values or two double values |
<type> <smaller> | The result has the type of the two numbers: an int for two int values |
<value> = Math.Min(<value>, <ceiling>); | A ceiling: after this line, <value> is never higher than <ceiling> |
The Example
A console app called MathMin compares three pairs of numbers: two positive, two negative and two double values. Then a shop with 10 notebooks takes three orders, and Math.Min makes sure it never sells more notebooks than it has left.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n MathMin
cd MathMin
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/MathMin/MathMin.csproj:
Determining projects to restore...
Restored /Users/you/source/MathMin/MathMin.csproj (in 42 ms).
Restore succeeded.
dotnet new console -n MathMin creates a folder named MathMin 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 MathMin.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First, three comparisons:
// Math.Min returns the smaller of two numbers.
Console.WriteLine($"Math.Min(7, 12) = {Math.Min(7, 12)}");
Console.WriteLine($"Math.Min(-3, -8) = {Math.Min(-3, -8)}");
Console.WriteLine($"Math.Min(2.5, 2.4) = {Math.Min(2.5, 2.4)}");
Each call gets two numbers and returns the smaller one. -3 and -8 are negative, and 2.5 and 2.4 are double values.
Then the ceiling:
// A ceiling: nobody can buy more notebooks than the shop has left.
int inStock = 10;
int[] orders = [4, 5, 3];
foreach (int wanted in orders)
{
int sold = Math.Min(wanted, inStock);
inStock -= sold;
Console.WriteLine($"Wanted {wanted}, sold {sold}, {inStock} left");
}
Math.Min(wanted, inStock) is how many notebooks the shop can sell: what the customer wants, but never more than what's left. inStock -= sold; takes the sold notebooks off the stock.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Math.Min(7, 12) = 7
Math.Min(-3, -8) = -8
Math.Min(2.5, 2.4) = 2.4
Wanted 4, sold 4, 6 left
Wanted 5, sold 5, 1 left
Wanted 3, sold 1, 0 left

Math.Min(7, 12) returned 7, and Math.Min(-3, -8) returned -8: of two negative numbers, the smaller is the one further from zero. For the two double values it returned 2.4. The first two orders were sold in full, leaving 6 notebooks and then 1. The last customer wanted 3, but only 1 was left, so Math.Min returned 1 and the stock ended at 0.
The Complete File

// Math.Min returns the smaller of two numbers.
Console.WriteLine($"Math.Min(7, 12) = {Math.Min(7, 12)}");
Console.WriteLine($"Math.Min(-3, -8) = {Math.Min(-3, -8)}");
Console.WriteLine($"Math.Min(2.5, 2.4) = {Math.Min(2.5, 2.4)}");
// A ceiling: nobody can buy more notebooks than the shop has left.
int inStock = 10;
int[] orders = [4, 5, 3];
foreach (int wanted in orders)
{
int sold = Math.Min(wanted, inStock);
inStock -= sold;
Console.WriteLine($"Wanted {wanted}, sold {sold}, {inStock} left");
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using Math.Max where you need a ceiling | With Math.Max(wanted, inStock), the first order of 4 sold 10 notebooks, and the stock went down to -5 and then -8 | Use Math.Min for a ceiling: the example sold 4, 5 and 1, and stopped at 0 |
| Passing three numbers | Math.Min(4, 9, 2) didn't build: error CS1501: No overload for method 'Min' takes 3 arguments | Nest the calls: Math.Min(4, Math.Min(9, 2)) returned 2. For an array, see the FAQ |
Calling Math.Min without keeping the result | Math.Min(battery, 100); on a line of its own left battery at 120 | Assign the result: after battery = Math.Min(battery, 100);, it printed 100 |
FAQ
How do I get the smallest number in an array?
Math.Min compares two numbers at a time. For a whole array, LINQ's Min() does it in one call:
Console.WriteLine($"Math.Min(4, Math.Min(9, 2)) = {Math.Min(4, Math.Min(9, 2))}");
int[] numbers = [4, 9, 2];
Console.WriteLine($"numbers.Min() = {numbers.Min()}");
Math.Min(4, Math.Min(9, 2)) = 2
numbers.Min() = 2
Both found 2. Min() comes from the System.Linq namespace, which a new console app already imports, so it needs no using line.
What does Math.Min return when one of the numbers is NaN?
NaN, the "not a number" value of double:
Console.WriteLine($"Math.Min(double.NaN, 5.0) = {Math.Min(double.NaN, 5.0)}");
Math.Min(double.NaN, 5.0) = NaN
The reference for the double version: Parameter val1 or val2, whichever is smaller. If val1, val2, or both val1 and val2 are equal to NaN, NaN is returned.
Conclusion
Math.Min(a, b) returns the smaller of two numbers of the same type. Write value = Math.Min(value, ceiling); to keep a value from going above a ceiling, nest calls for more than two numbers, and use LINQ's Min() for a whole collection.
Additional Resources
Math.MinMethod — every overload, and how thedoubleversion treats NaN.Math.MaxMethod — the larger of two numbers, for a floor.Enumerable.MinMethod — the smallest value of a whole collection.
