C# Reference ยท Numbers and Math
Math.Min() and Math.Max() methods
Reference for Math.Min and Math.Max in C#: smaller and larger of two values, NaN and negative zero rules, MinMagnitude, LINQ Min and Max for sequences.
Math.Min(a, b) returns the smaller of two numbers and Math.Max(a, b) the larger. Both exist for every built-in numeric type (int, long, double, decimal, byte and so on), and both arguments must be the same type after implicit conversion. They compare exactly two values; for a whole collection use LINQ's Min() and Max().
Together they are the building blocks of clamping: Math.Max(low, Math.Min(x, high)) forces x into a range, and Math.Clamp(x, low, high) does the same in one call.
int smaller = Math.Min(4, 9); // 4 int larger = Math.Max(4, 9); // 9
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static int Min(int val1, int val2); public static long Min(long val1, long val2); public static double Min(double val1, double val2); public static decimal Min(decimal val1, decimal val2); public static nint Min(nint val1, nint val2); // ... and the same for byte, sbyte, short, ushort, uint, ulong, float, nuint // Max has the identical set of overloads public static double MinMagnitude(double x, double y); public static double MaxMagnitude(double x, double y);
| Name | Description |
|---|---|
val1, val2 |
The two values to compare. Both are converted to a common type by the usual numeric conversion rules. |
Returns: the smaller (Min) or larger (Max) value, as the common type. MinMagnitude and MaxMagnitude return the argument with the smaller or larger absolute value.
Errors / undefined behavior: none for the two-argument methods. For floating point, NaN in either position gives NaN. For LINQ, Min() and Max() on an empty sequence of a non-nullable type throw InvalidOperationException.
Usage notes
- Two arguments only. Chain calls for three or more, or put the values in an array and use
Enumerable.Max. For many numbers, a loop is faster than creating an array. - Mixed types promote.
Math.Max(10, 3.5)selects thedoubleoverload, so the int becomes10.0.Math.Max(1, 2L)returns along. If no implicit conversion exists (for examplelongandulong), the call does not compile. - Floating-point edge cases.
Math.Max(double.NaN, 1)isNaN, unlike the Cfmaxfunction, which ignores NaN.-0.0is considered smaller than0.0. The<and>operators treat both zeros as equal and givefalsefor any comparison with NaN. - Clamping. Prefer
Math.Clamp()over nested Min and Max. It throwsArgumentExceptionif the minimum is greater than the maximum, which catches mixed-up arguments. - Generic math.
double.Max,int.Min, andT.Maxfor anyINumber<T>give the same behavior in generic code.double.MaxNumberanddouble.MinNumberreturn the non-NaN value if only one is NaN. - Dates and strings.
Math.Minis for numbers. ForDateTime, useDateTime.Compareor LINQMin(); for strings,Max()uses the default culture-aware comparer, so use ordinal comparison when you need predictable results. - Performance. The integer overloads compile to a compare and a conditional move; call them in loops freely. Vectorized versions exist as
Vector.MinandVector.Max.