Edit

Share via


Math.Min Method

Definition

Returns the smaller of two numbers.

Overloads

Min(UInt32, UInt32)

Returns the smaller of two 32-bit unsigned integers.

Min(UInt16, UInt16)

Returns the smaller of two 16-bit unsigned integers.

Min(Single, Single)

Returns the smaller of two single-precision floating-point numbers.

Min(SByte, SByte)

Returns the smaller of two 8-bit signed integers.

Min(IntPtr, IntPtr)

Returns the smaller of two native signed integers.

Min(Double, Double)

Returns the smaller of two double-precision floating-point numbers.

Min(Int32, Int32)

Returns the smaller of two 32-bit signed integers.

Min(Int16, Int16)

Returns the smaller of two 16-bit signed integers.

Min(Decimal, Decimal)

Returns the smaller of two decimal numbers.

Min(Byte, Byte)

Returns the smaller of two 8-bit unsigned integers.

Min(UInt64, UInt64)

Returns the smaller of two 64-bit unsigned integers.

Min(Int64, Int64)

Returns the smaller of two 64-bit signed integers.

Min(UIntPtr, UIntPtr)

Returns the smaller of two native unsigned integers.

Examples

The following example demonstrates how to use the Min method to return and display the smaller of two variables:

C#
string str = "{0}: The lesser of {1,3} and {2,3} is {3}.";

byte     xByte1    = 1,    xByte2    = 51;	
short    xShort1   = -2,   xShort2   = 52;
int      xInt1     = -3,   xInt2     = 53;
long     xLong1    = -4,   xLong2    = 54;
float    xSingle1  = 5.0f, xSingle2  = 55.0f;
double   xDouble1  = 6.0,  xDouble2  = 56.0;
Decimal  xDecimal1 = 7m,   xDecimal2 = 57m;

// The following types are not CLS-compliant.
sbyte    xSbyte1   = 101, xSbyte2  = 111;
ushort   xUshort1  = 102, xUshort2 = 112;
uint     xUint1    = 103, xUint2   = 113;
ulong    xUlong1   = 104, xUlong2  = 114;

Console.WriteLine("Display the lesser of two values:\n");
Console.WriteLine(str, "Byte   ", xByte1, xByte2, Math.Min(xByte1, xByte2));
Console.WriteLine(str, "Int16  ", xShort1, xShort2, Math.Min(xShort1, xShort2));
Console.WriteLine(str, "Int32  ", xInt1, xInt2, Math.Min(xInt1, xInt2));
Console.WriteLine(str, "Int64  ", xLong1, xLong2, Math.Min(xLong1, xLong2));
Console.WriteLine(str, "Single ", xSingle1, xSingle2, Math.Min(xSingle1, xSingle2));
Console.WriteLine(str, "Double ", xDouble1, xDouble2, Math.Min(xDouble1, xDouble2));
Console.WriteLine(str, "Decimal", xDecimal1, xDecimal2, Math.Min(xDecimal1, xDecimal2));

Console.WriteLine("\nThe following types are not CLS-compliant:\n");
Console.WriteLine(str, "SByte  ", xSbyte1, xSbyte2, Math.Min(xSbyte1, xSbyte2));
Console.WriteLine(str, "UInt16 ", xUshort1, xUshort2, Math.Min(xUshort1, xUshort2));
Console.WriteLine(str, "UInt32 ", xUint1, xUint2, Math.Min(xUint1, xUint2));
Console.WriteLine(str, "UInt64 ", xUlong1, xUlong2, Math.Min(xUlong1, xUlong2));

/*
This example produces the following results:

Display the lesser of two values:

Byte   : The lesser of   1 and  51 is 1.
Int16  : The lesser of  -2 and  52 is -2.
Int32  : The lesser of  -3 and  53 is -3.
Int64  : The lesser of  -4 and  54 is -4.
Single : The lesser of   5 and  55 is 5.
Double : The lesser of   6 and  56 is 6.
Decimal: The lesser of   7 and  57 is 7.

The following types are not CLS-compliant:

SByte  : The lesser of 101 and 111 is 101.
UInt16 : The lesser of 102 and 112 is 102.
UInt32 : The lesser of 103 and 113 is 103.
UInt64 : The lesser of 104 and 114 is 104.
*/

Min(UInt32, UInt32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the smaller of two 32-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static uint Min(uint val1, uint val2);

Parameters

val1
UInt32

The first of two 32-bit unsigned integers to compare.

val2
UInt32

The second of two 32-bit unsigned integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(UInt16, UInt16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the smaller of two 16-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static ushort Min(ushort val1, ushort val2);

Parameters

val1
UInt16

The first of two 16-bit unsigned integers to compare.

val2
UInt16

The second of two 16-bit unsigned integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Single, Single)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two single-precision floating-point numbers.

C#
public static float Min(float val1, float val2);

Parameters

val1
Single

The first of two single-precision floating-point numbers to compare.

val2
Single

The second of two single-precision floating-point numbers to compare.

Returns

Parameter val1 or val2, whichever is smaller. If val1, val2, or both val1 and val2 are equal to NaN, NaN is returned.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(SByte, SByte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the smaller of two 8-bit signed integers.

C#
[System.CLSCompliant(false)]
public static sbyte Min(sbyte val1, sbyte val2);

Parameters

val1
SByte

The first of two 8-bit signed integers to compare.

val2
SByte

The second of two 8-bit signed integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(IntPtr, IntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two native signed integers.

C#
public static IntPtr Min(IntPtr val1, IntPtr val2);
C#
public static nint Min(nint val1, nint val2);

Parameters

val1

nint

The first of two native signed integers to compare.

val2

nint

The second of two native signed integers to compare.

Returns

IntPtr

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10

Min(Double, Double)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two double-precision floating-point numbers.

C#
public static double Min(double val1, double val2);

Parameters

val1
Double

The first of two double-precision floating-point numbers to compare.

val2
Double

The second of two double-precision floating-point numbers to compare.

Returns

Parameter val1 or val2, whichever is smaller. If val1, val2, or both val1 and val2 are equal to NaN, NaN is returned.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Int32, Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two 32-bit signed integers.

C#
public static int Min(int val1, int val2);

Parameters

val1
Int32

The first of two 32-bit signed integers to compare.

val2
Int32

The second of two 32-bit signed integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Int16, Int16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two 16-bit signed integers.

C#
public static short Min(short val1, short val2);

Parameters

val1
Int16

The first of two 16-bit signed integers to compare.

val2
Int16

The second of two 16-bit signed integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Decimal, Decimal)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two decimal numbers.

C#
public static decimal Min(decimal val1, decimal val2);

Parameters

val1
Decimal

The first of two decimal numbers to compare.

val2
Decimal

The second of two decimal numbers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Byte, Byte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two 8-bit unsigned integers.

C#
public static byte Min(byte val1, byte val2);

Parameters

val1
Byte

The first of two 8-bit unsigned integers to compare.

val2
Byte

The second of two 8-bit unsigned integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(UInt64, UInt64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the smaller of two 64-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static ulong Min(ulong val1, ulong val2);

Parameters

val1
UInt64

The first of two 64-bit unsigned integers to compare.

val2
UInt64

The second of two 64-bit unsigned integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(Int64, Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the smaller of two 64-bit signed integers.

C#
public static long Min(long val1, long val2);

Parameters

val1
Int64

The first of two 64-bit signed integers to compare.

val2
Int64

The second of two 64-bit signed integers to compare.

Returns

Parameter val1 or val2, whichever is smaller.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Min(UIntPtr, UIntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the smaller of two native unsigned integers.

C#
[System.CLSCompliant(false)]
public static UIntPtr Min(UIntPtr val1, UIntPtr val2);
C#
[System.CLSCompliant(false)]
public static nuint Min(nuint val1, nuint val2);

Parameters

val1

nuint

The first of two native unsigned integers to compare.

val2

nuint

The second of two native unsigned integers to compare.

Returns

UIntPtr

Parameter val1 or val2, whichever is smaller.

Attributes

Applies to

.NET 10 and other versions
Product Versions
.NET 6, 7, 8, 9, 10