Čítať v angličtine Upraviť

Zdieľať cez


Math.Max Method

Definition

Returns the larger of two specified numbers.

Overloads

Max(UInt64, UInt64)

Returns the larger of two 64-bit unsigned integers.

Max(UInt32, UInt32)

Returns the larger of two 32-bit unsigned integers.

Max(UInt16, UInt16)

Returns the larger of two 16-bit unsigned integers.

Max(Single, Single)

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

Max(SByte, SByte)

Returns the larger of two 8-bit signed integers.

Max(IntPtr, IntPtr)

Returns the larger of two native signed integers.

Max(UIntPtr, UIntPtr)

Returns the larger of two native unsigned integers.

Max(Int32, Int32)

Returns the larger of two 32-bit signed integers.

Max(Int16, Int16)

Returns the larger of two 16-bit signed integers.

Max(Double, Double)

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

Max(Decimal, Decimal)

Returns the larger of two decimal numbers.

Max(Byte, Byte)

Returns the larger of two 8-bit unsigned integers.

Max(Int64, Int64)

Returns the larger of two 64-bit signed integers.

Examples

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

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

Important

This API is not CLS-compliant.

Returns the larger of two 64-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static ulong Max(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 larger.

Attributes

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(UInt32, UInt32)

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

Important

This API is not CLS-compliant.

Returns the larger of two 32-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static uint Max(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 larger.

Attributes

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(UInt16, UInt16)

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

Important

This API is not CLS-compliant.

Returns the larger of two 16-bit unsigned integers.

C#
[System.CLSCompliant(false)]
public static ushort Max(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 larger.

Attributes

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Single, Single)

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

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

C#
public static float Max(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 larger. If val1, or val2, or both val1 and val2 are equal to NaN, NaN is returned.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(SByte, SByte)

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

Important

This API is not CLS-compliant.

Returns the larger of two 8-bit signed integers.

C#
[System.CLSCompliant(false)]
public static sbyte Max(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 larger.

Attributes

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(IntPtr, IntPtr)

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

Returns the larger of two native signed integers.

C#
public static IntPtr Max(IntPtr val1, IntPtr val2);
C#
public static nint Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET 6, 7, 8, 9, 10

Max(UIntPtr, UIntPtr)

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

Important

This API is not CLS-compliant.

Returns the larger of two native unsigned integers.

C#
[System.CLSCompliant(false)]
public static UIntPtr Max(UIntPtr val1, UIntPtr val2);
C#
[System.CLSCompliant(false)]
public static nuint Max(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 larger.

Attributes

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET 6, 7, 8, 9, 10

Max(Int32, Int32)

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

Returns the larger of two 32-bit signed integers.

C#
public static int Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Int16, Int16)

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

Returns the larger of two 16-bit signed integers.

C#
public static short Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Double, Double)

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

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

C#
public static double Max(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 larger. If val1, val2, or both val1 and val2 are equal to NaN, NaN is returned.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Decimal, Decimal)

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

Returns the larger of two decimal numbers.

C#
public static decimal Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Byte, Byte)

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

Returns the larger of two 8-bit unsigned integers.

C#
public static byte Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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

Max(Int64, Int64)

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

Returns the larger of two 64-bit signed integers.

C#
public static long Max(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 larger.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.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