In C#, Math.Min(a, b) compares two numbers and returns the smaller one. Math.Min(10, 20) is 10.
Think of a lift with a sign "maximum 8 people". If 12 people want to ride, only Math.Min(12, 8) = 8 can. Min is the natural way to put an upper limit on a number. It is the partner of Max(), and the two together build a range, which Clamp() does in one call.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Min(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Min is in every .NET version, with overloads for all the common number types.
Syntax
Pass two numbers of the same type. You get the smaller one back.
C#
int a = Math.Min(10, 20); // 10
double b = Math.Min(-1.5, -2.5); // -2.5
long c = Math.Min(5L, 3L); // 3
Call
What it does
Math.Min(10, 20)
Returns 10.
Math.Min(-5, -9)
Returns -9. With negatives, the one farther from zero is smaller.
Math.Min(7, 2.0)
Mixes int and double. The double overload runs and returns 2.
Math.Min(double.NaN, 5)
Returns NaN. If either input is NaN, the answer is NaN.
Both numbers should have the same type, or one that widens to the other. The method returns one of the two numbers you passed. It never changes your variables. To keep a value at or below a limit, write value = Math.Min(value, limit);.
Library declarations
C#
public static int Min(int val1, int val2);
public static long Min(long val1, long val2);
public static float Min(float val1, float val2);
public static double Min(double val1, double val2);
public static decimal Min(decimal val1, decimal val2);
// also byte, sbyte, short, ushort, uint, ulong, nint, nuint overloads
Parameters
Parameter
Description
val1
Required. The first number to compare.
val2
Required. The second number to compare, of the same type as val1 (or a type it can widen to).
Return Value
Return Value
Description
Same type as the inputs
The smaller of val1 and val2. For double and float, the result is NaN if either input is NaN.
C# Examples
Example 1 — Pick the Smaller Number
Call Min with positive numbers, negative numbers, and a mix of types.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Min(10, 20));
Console.WriteLine(Math.Min(-5, -9));
Console.WriteLine(Math.Min(0, 4));
Console.WriteLine(Math.Min(7, 2.0));
}
}
Output
10
-9
0
2
How It Works
Two ints.Math.Min(10, 20) picks the smaller value, 10.
Negatives. -9 is to the left of -5 on the number line, so -9 is the smaller number.
Zero is in play. Zero is smaller than 4, so Math.Min(0, 4) returns 0.
Mixed types. The int 7 is widened to a double. The smaller value is 2.0, and Console prints it as 2.
Example 2 — Never Go Over the Limit
Use Min to cap a value from above, and Min with Max to keep a value inside a range.
C#
using System;
class Program {
static void Main() {
int stock = 30;
int ordered = 45;
int shipped = Math.Min(stock, ordered);
Console.WriteLine(shipped);
int tooHigh = 120;
int tooLow = -8;
Console.WriteLine(Math.Min(Math.Max(tooHigh, 0), 100));
Console.WriteLine(Math.Min(Math.Max(tooLow, 0), 100));
}
}
Output
30
100
0
How It Works
Ship what you have. The customer ordered 45, but only 30 are in stock. Math.Min(stock, ordered) returns 30, the most you can send.
Max first, Min second.Math.Max(tooHigh, 0) lifts anything below 0 up to 0. Then Math.Min(..., 100) pulls anything above 100 down to 100.
120 is too high.Max(120, 0) stays 120, then Min(120, 100) gives 100.
-8 is too low.Max(-8, 0) gives 0, then Min(0, 100) keeps 0. This pair does the same job as Math.Clamp(value, 0, 100).
Example 3 — NaN, Infinity, and Big Numbers
See how Min treats NaN, infinity, and a long that is far bigger than an int can hold.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Min(double.NaN, 5));
Console.WriteLine(Math.Min(5, double.NaN));
Console.WriteLine(Math.Min(double.PositiveInfinity, 99));
Console.WriteLine(Math.Min(int.MinValue, 0));
Console.WriteLine(Math.Min(5000000000L, 7L));
}
}
Output
NaN
NaN
99
-2147483648
7
How It Works
NaN first.Math.Min(double.NaN, 5) returns NaN, not 5. A NaN input wins.
NaN second. Swapping the order changes nothing: Math.Min(5, double.NaN) is NaN too.
Infinity. Positive infinity is bigger than every number, so 99 is the smaller one. Negative infinity would win against any number.
Whole-number limits.int.MinValue is -2147483648, smaller than 0. The long pair picks 7 over five billion, since 7 is the smaller number.
Frequently Asked Questions
It returns the smaller of two numbers. Math.Min(10, 20) is 10. It works with int, long, double, decimal, and other number types.
Use value = Math.Min(value, limit). To also stop it going below a lower limit, add Math.Max, or use Math.Clamp(value, lower, upper) in one call.
Min compares by value, so Min(-8, 5) is -8. MinMagnitude compares by distance from zero and ignores the sign when choosing, so MinMagnitude(-8, 5) is 5.
No. Math is a static class, so you call Math.Min(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.
For double and float, it returns NaN. Check double.IsNaN on your data first if it may contain NaN.