Math.Max 方法

定义

返回两个指定数字中的较大值。

重载

Max(UInt64, UInt64)

返回两个 64 位无符号整数中的较大值。

Max(UInt32, UInt32)

返回两个 32 位无符号整数中的较大值。

Max(UInt16, UInt16)

返回两个 16 位无符号整数中的较大值。

Max(Single, Single)

返回两个单精度浮点数中的较大值。

Max(SByte, SByte)

返回两个 8 位有符号整数中的较大值。

Max(IntPtr, IntPtr)

返回两个本机有符号整数中的较大值。

Max(UIntPtr, UIntPtr)

返回两个本机无符号整数中的较大值。

Max(Int32, Int32)

返回两个 32 位有符号整数中的较大值。

Max(Int16, Int16)

返回两个 16 位有符号整数中的较大值。

Max(Double, Double)

返回两个双精度浮点数中的较大值。

Max(Decimal, Decimal)

返回两个十进制数字中的较大值。

Max(Byte, Byte)

返回两个 8 位无符号整数中的较大值。

Max(Int64, Int64)

返回两个 64 位有符号整数中的较大值。

示例

以下示例演示如何使用 Max 方法返回和显示两个变量中的更大值:

C#
string str = "{0}: The greater 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 greater of two values:\n");
Console.WriteLine(str, "Byte   ", xByte1, xByte2, Math.Max(xByte1, xByte2));
Console.WriteLine(str, "Int16  ", xShort1, xShort2, Math.Max(xShort1, xShort2));
Console.WriteLine(str, "Int32  ", xInt1, xInt2, Math.Max(xInt1, xInt2));
Console.WriteLine(str, "Int64  ", xLong1, xLong2, Math.Max(xLong1, xLong2));
Console.WriteLine(str, "Single ", xSingle1, xSingle2, Math.Max(xSingle1, xSingle2));
Console.WriteLine(str, "Double ", xDouble1, xDouble2, Math.Max(xDouble1, xDouble2));
Console.WriteLine(str, "Decimal", xDecimal1, xDecimal2, Math.Max(xDecimal1, xDecimal2));

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

/*
This example produces the following results:

Display the greater of two values:

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

(The following types are not CLS-compliant.)

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

Max(UInt64, UInt64)

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

重要

此 API 不符合 CLS。

返回两个 64 位无符号整数中的较大值。

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

参数

val1
UInt64

要比较的两个 64 位无符号整数中的第一个。

val2
UInt64

要比较的两个 64 位无符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

属性

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(UInt32, UInt32)

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

重要

此 API 不符合 CLS。

返回两个 32 位无符号整数中的较大值。

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

参数

val1
UInt32

要比较的两个 32 位无符号整数中的第一个。

val2
UInt32

要比较的两个 32 位无符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

属性

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(UInt16, UInt16)

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

重要

此 API 不符合 CLS。

返回两个 16 位无符号整数中的较大值。

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

参数

val1
UInt16

要比较的两个 16 位无符号整数中的第一个。

val2
UInt16

要比较的两个 16 位无符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

属性

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Single, Single)

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

返回两个单精度浮点数中的较大值。

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

参数

val1
Single

要比较的两个单精度浮点数中的第一个。

val2
Single

要比较的两个单精度浮点数中的第二个。

返回

参数 val1val2,以较大者为准。 如果 val1val2,或者 val1val2 都等于 NaN,则返回 NaN

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(SByte, SByte)

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

重要

此 API 不符合 CLS。

返回两个 8 位有符号整数中的较大值。

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

参数

val1
SByte

要比较的两个 8 位有符号整数中的第一个。

val2
SByte

要比较的两个 8 位有符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

属性

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(IntPtr, IntPtr)

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

返回两个本机有符号整数中的较大值。

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

参数

val1

nint

要比较的两个本机有符号整数中的第一个。

val2

nint

要比较的两个本机有符号整数中的第二个。

返回

nint

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Max(UIntPtr, UIntPtr)

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

重要

此 API 不符合 CLS。

返回两个本机无符号整数中的较大值。

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

参数

val1

nuint

要比较的两个本机无符号整数中的第一个。

val2

nuint

要比较的两个本机无符号整数中的第二个。

返回

nuint

参数 val1val2,以较大者为准。

属性

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Max(Int32, Int32)

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

返回两个 32 位有符号整数中的较大值。

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

参数

val1
Int32

要比较的两个 32 位有符号整数中的第一个。

val2
Int32

要比较的两个 32 位带符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Int16, Int16)

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

返回两个 16 位有符号整数中的较大值。

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

参数

val1
Int16

要比较的两个 16 位有符号整数中的第一个。

val2
Int16

要比较的两个 16 位带符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Double, Double)

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

返回两个双精度浮点数中的较大值。

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

参数

val1
Double

要比较的两个双精度浮点数中的第一个。

val2
Double

要比较的两个双精度浮点数中的第二个。

返回

参数 val1val2,以较大者为准。 如果 val1val2或两者 val1val2 都等于 NaN,则返回 NaN

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Decimal, Decimal)

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

返回两个十进制数字中的较大值。

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

参数

val1
Decimal

要比较的两个十进制数中的第一个。

val2
Decimal

要比较的两个十进制数中的第二个。

返回

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Byte, Byte)

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

返回两个 8 位无符号整数中的较大值。

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

参数

val1
Byte

要比较的两个 8 位无符号整数中的第一个。

val2
Byte

要比较的两个 8 位无符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.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
.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

Max(Int64, Int64)

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

返回两个 64 位有符号整数中的较大值。

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

参数

val1
Int64

要比较的两个 64 位带符号整数中的第一个。

val2
Int64

要比较的两个 64 位带符号整数中的第二个。

返回

参数 val1val2,以较大者为准。

适用于

.NET 9 和其他版本
产品 版本
.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
.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