Open In App

C# | Math.Max() Method

Last Updated : 13 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Max() is a Math class method which is used to returns the larger of the two specified numbers. This method always takes two arguments and it can be overloaded by changing the data type of the passed arguments as follows:
 

  • Math.Max(Byte, Byte): Returns the larger of the two 8-bit unsigned integers.
  • Math.Max(Decimal, Decimal): Returns the larger of the two decimal numbers.
  • Math.Max(Double, Double): Returns the larger of the two double-precision floating-point numbers.
  • Math.Max(Int16, Int16): Returns the larger of the two 16-bit signed integers. Here Int16 is short data type.
  • Math.Max(Int32, Int32): Returns the larger of the two 32-bit signed integers. Here Int32 is int data type.
  • Math.Max(Int64, Int64): Returns the larger of the two 64-bit signed integers. Here Int64 is long data type.
  • Math.Max(SByte, SByte): Returns the larger of the two 8-bit signed integers.
  • Math.Max(single, single): Returns the larger of the two single-precision floating-point numbers. Here single is float data type.
  • Math.Max(UInt16, UInt16): Returns the larger of the two 16-bit unsigned integers. Here UInt16 is unsigned short(ushort) data type.
  • Math.Max(UInt32, UInt32): Returns the larger of the two 32-bit unsigned integers. Here UInt32 is unsigned int(uint) data type.
  • Math.Max(UInt64, UInt64): Returns the larger of the two 64-bit unsigned integers. Here UInt64 is unsigned long(ulong) data type.

Common Syntax for all above methods:
 

public static data_type Max(Data_type first_value, Data_type second_value)

Parameter:
 

These methods always accept the two parameters of the specified data type.

Return Type: Methods return the maximum of the two numbers which specified into the parameter list and return type is depends on the type of arguments passed.
Example:
 

CSharp




// C# program to demonstrate the
// Math.Max() method
using System;
class GFG {
 
// Main Method
static void Main()
{
     
    // byte data type
    byte b1 = 10, b2 = 15;
     
    // decimal data type
    decimal d1 = 1000M, d2 = 1568M;
     
    // double data type
    double db1 = 15.896745, db2 = 8.62644598;
     
     
    // Int16 data type
    short sh1 = -96, sh2 = 24;
 
    // Int32 data type
    int i1 = 26745236, i2 = 36725413;
     
     
    // Int64 data type
    long l1 = -2534234234234, l2 = -745837587527423;
     
    // sbyte data type
    sbyte sb1 = 52, sb2 = 120;
     
    // single data type
    float f1 = 8.0f, f2 = 78.78f;
     
    // UInt16 data type
    ushort us1 = 5346, us2 = 6437;
     
    // UInt32 data type
    uint un1 = 432344637, un2 = 64762738;
     
    // UInt64 data type
    ulong ul1 = 34234234, ul2 = 673286478326;
     
 
    // displaying result
    Console.WriteLine("Math.Max Method (Byte, Byte) = " + Math.Max(b1, b2));
    Console.WriteLine("Math.Max Method (Decimal, Decimal) = " + Math.Max(d1, d2));
    Console.WriteLine("Math.Max Method (Double, Double) = " + Math.Max(db1, db2));
    Console.WriteLine("Math.Max Method (Int16, Int16) = " + Math.Max(sh1, sh2));
    Console.WriteLine("Math.Max Method (Int32, Int32) = " + Math.Max(i1, i2));
    Console.WriteLine("Math.Max Method (Int64, lInt64) = " + Math.Max(l1, l2));
    Console.WriteLine("Math.Max Method (SByte, SByte) = " + Math.Max(sb1, sb2));
    Console.WriteLine("Math.Max Method (Single, Single) = " + Math.Max(f1, f2));
    Console.WriteLine("Math.Max Method (UInt16, UInt16) = " + Math.Max(us1, us2));
    Console.WriteLine("Math.Max Method (UInt32, UInt32) = " + Math.Max(un1, un2));
    Console.WriteLine("Math.Max Method (UInt64, UInt64) = " + Math.Max(ul1, ul2));
     
    }
}


Output: 

Math.Max Method (Byte, Byte) = 15
Math.Max Method (Decimal, Decimal) = 1568
Math.Max Method (Double, Double) = 15.896745
Math.Max Method (Int16, Int16) = 24
Math.Max Method (Int32, Int32) = 36725413
Math.Max Method (Int64, lInt64) = -2534234234234
Math.Max Method (SByte, SByte) = 120
Math.Max Method (Single, Single) = 78.78
Math.Max Method (UInt16, UInt16) = 6437
Math.Max Method (UInt32, UInt32) = 432344637
Math.Max Method (UInt64, UInt64) = 673286478326

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads