Export (0) Print
Expand All

Math.Max Method

Returns the larger of two specified numbers.

Overload List

Returns the larger of two 8-bit unsigned integers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Byte, Byte) As Byte
[C#] public static byte Max(byte, byte);
[C++] public: static unsigned char Max(unsigned char, unsigned char);
[JScript] public static function Max(Byte, Byte) : Byte;

Returns the larger of two decimal numbers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Decimal, Decimal) As Decimal
[C#] public static decimal Max(decimal, decimal);
[C++] public: static Decimal Max(Decimal, Decimal);
[JScript] public static function Max(Decimal, Decimal) : Decimal;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Double, Double) As Double
[C#] public static double Max(double, double);
[C++] public: static double Max(double, double);
[JScript] public static function Max(double, double) : double;

Returns the larger of two 16-bit signed integers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Short, Short) As Short
[C#] public static short Max(short, short);
[C++] public: static short Max(short, short);
[JScript] public static function Max(Int16, Int16) : Int16;

Returns the larger of two 32-bit signed integers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Integer, Integer) As Integer
[C#] public static int Max(int, int);
[C++] public: static int Max(int, int);
[JScript] public static function Max(int, int) : int;

Returns the larger of two 64-bit signed integers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Long, Long) As Long
[C#] public static long Max(long, long);
[C++] public: static __int64 Max(__int64, __int64);
[JScript] public static function Max(long, long) : long;

Returns the larger of two 8-bit signed integers. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(SByte, SByte) As SByte
[C#] public static sbyte Max(sbyte, sbyte);
[C++] public: static char Max(char, char);
[JScript] public static function Max(SByte, SByte) : SByte;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(Single, Single) As Single
[C#] public static float Max(float, float);
[C++] public: static float Max(float, float);
[JScript] public static function Max(float, float) : float;

Returns the larger of two 16-bit unsigned integers. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(UInt16, UInt16) As UInt16
[C#] public static ushort Max(ushort, ushort);
[C++] public: static unsigned short Max(unsigned short, unsigned short);
[JScript] public static function Max(UInt16, UInt16) : UInt16;

Returns the larger of two 32-bit unsigned integers. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(UInt32, UInt32) As UInt32
[C#] public static uint Max(uint, uint);
[C++] public: static unsigned int Max(unsigned int, unsigned int);
[JScript] public static function Max(UInt32, UInt32) : UInt32;

Returns the larger of two 64-bit unsigned integers. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Max(UInt64, UInt64) As UInt64
[C#] public static ulong Max(ulong, ulong);
[C++] public: static unsigned __int64 Max(unsigned __int64, unsigned __int64);
[JScript] public static function Max(UInt64, UInt64) : UInt64;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Max. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' This example demonstrates Math.Max()
Imports System

Class Sample
   Public Shared Sub Main()
      Dim str As String = "{0}: The greater of {1,3} and {2,3} is {3}."
      Dim nl As String = Environment.NewLine
      
      Dim xByte1 As Byte = 1
      Dim xByte2 As Byte = 51
      Dim xShort1 As Short = - 2
      Dim xShort2 As Short = 52
      Dim xInt1 As Integer = - 3
      Dim xInt2 As Integer = 53
      Dim xLong1 As Long = - 4
      Dim xLong2 As Long = 54
      Dim xSingle1 As Single = 5F
      Dim xSingle2 As Single = 55F
      Dim xDouble1 As Double = 6.0
      Dim xDouble2 As Double = 56.0
      Dim xDecimal1 As [Decimal] = 7D
      Dim xDecimal2 As [Decimal] = 57D
      
      ' The following types are not CLS-compliant: 
      ' SByte, UInt16, UInt32, and UInt64.
      
      Console.WriteLine("{0}Display the greater of two values:{0}", nl)
      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.Write("{0}The following types are not supported. ", nl)
      Console.WriteLine("SByte, UInt16, UInt32, and UInt64.")
   End Sub 'Main
End Class 'Sample
'
'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 supported: SByte, UInt16, UInt32, and UInt64.
'

[C#] 
// This example demonstrates Math.Max()
using System;

class Sample 
{
    public static void Main() 
    {
    string str = "{0}: The greater of {1,3} and {2,3} is {3}.";
    string nl = Environment.NewLine;

    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("{0}Display the greater of two values:{0}", nl);
    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("{0}The following types are not CLS-compliant.{0}", nl);
    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.
*/

[C++] 
// This example demonstrates Math.Max()
#using <mscorlib.dll>
using namespace System;

int main() 
{
    String* str = S"{0}: The greater of {1,3} and {2,3} is {3}.";
    String* nl = Environment::NewLine;

    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 = 7,   xDecimal2  = 57;

// The following types are not CLS-compliant.
    SByte    xSbyte1   = 101, xSbyte2  = 111;
    UInt16   xUshort1  = 102, xUshort2 = 112;
    UInt32     xUint1    = 103, xUint2   = 113;
    UInt64    xUlong1   = 104, xUlong2  = 114;

    Console::WriteLine(S"{0}Display the greater of two values:{0}", nl);
    Console::WriteLine(str, S"Byte   ", __box(xByte1), __box(xByte2), __box(Math::Max(xByte1, xByte2)));
    Console::WriteLine(str, S"Int16  ", __box(xShort1), __box(xShort2), __box(Math::Max(xShort1, xShort2)));
    Console::WriteLine(str, S"Int32  ", __box(xInt1), __box(xInt2), __box(Math::Max(xInt1, xInt2)));
    Console::WriteLine(str, S"Int64  ", __box(xLong1), __box(xLong2), __box(Math::Max(xLong1, xLong2)));
    Console::WriteLine(str, S"Single ", __box(xSingle1), __box(xSingle2), __box(Math::Max(xSingle1, xSingle2)));
    Console::WriteLine(str, S"Double ", __box(xDouble1), __box(xDouble2), __box(Math::Max(xDouble1, xDouble2)));
    Console::WriteLine(str, S"Decimal", __box(xDecimal1), __box(xDecimal2), __box(Math::Max(xDecimal1, xDecimal2)));
//
    Console::WriteLine(S"{0}The following types are not CLS-compliant.{0}", nl);
    Console::WriteLine(str, S"SByte  ", __box(xSbyte1), __box(xSbyte2), __box(Math::Max(xSbyte1, xSbyte2)));
    Console::WriteLine(str, S"UInt16 ", __box(xUshort1), __box(xUshort2), __box(Math::Max(xUshort1, xUshort2)));
    Console::WriteLine(str, S"UInt32 ", __box(xUint1), __box(xUint2), __box(Math::Max(xUint1, xUint2)));
    Console::WriteLine(str, S"UInt64 ", __box(xUlong1), __box(xUlong2), __box(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.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Math Class | Math Members | System Namespace

Show:
© 2015 Microsoft