Skip to content
Tutorials Center
My learning

Topics

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.

C#
int smaller = Math.Min(4, 9);   // 4
int larger = Math.Max(4, 9);    // 9

Try it

C# Min and Max on numbers

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.

C# NaN, negative zero and sequences

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

C#
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 the double overload, so the int becomes 10.0. Math.Max(1, 2L) returns a long. If no implicit conversion exists (for example long and ulong), the call does not compile.
  • Floating-point edge cases. Math.Max(double.NaN, 1) is NaN, unlike the C fmax function, which ignores NaN. -0.0 is considered smaller than 0.0. The < and > operators treat both zeros as equal and give false for any comparison with NaN.
  • Clamping. Prefer Math.Clamp() over nested Min and Max. It throws ArgumentException if the minimum is greater than the maximum, which catches mixed-up arguments.
  • Generic math. double.Max, int.Min, and T.Max for any INumber<T> give the same behavior in generic code. double.MaxNumber and double.MinNumber return the non-NaN value if only one is NaN.
  • Dates and strings. Math.Min is for numbers. For DateTime, use DateTime.Compare or LINQ Min(); 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.Min and Vector.Max.