C# Tutorial C# Advanced C# References

C# Math - Max() Method



The C# Max() method returns maximum number between the two arguments. The method can be overloaded and it can take long, decimal, double, float, int, short, byte, ulong, uint, ushort, and sbyte arguments. If either of the arguments is NaN, the method returns NaN.

Syntax

public static ulong Max (ulong arg1, ulong arg2);
public static uint Max (uint arg1, uint arg2);
public static ushort Max (ushort arg1, ushort arg2);
public static float Max (float arg1, float arg2);
public static sbyte Max (sbyte arg1, sbyte arg2);
public static byte Max (byte arg1, byte arg2);
public static int Max (int arg1, int arg2);
public static short Max (short arg1, short arg2);
public static double Max (double arg1, double arg2);
public static decimal Max (decimal arg1, decimal arg2);
public static long Max (long arg1, long arg2);

Parameters

arg1 Specify value to compare.
arg2 Specify value to compare.

Return Value

Returns the numerically maximum value.

Example:

In the example below, Max() method is used to find out the maximum value between the two arguments.

using System;

class MyProgram {
  static void Main(string[] args) {
   Console.WriteLine("Math.Max(50, 100) returns: " 
                     + Math.Max(50, 100));
   Console.WriteLine("Math.Max(5.5f, 10.5f) returns: " 
                     + Math.Max(5.5f, 10.5f));
   Console.WriteLine("Math.Max(5, 10.5f) returns: " 
                     + Math.Max(5, 10.5f));
   Console.WriteLine("Math.Max(10, Double.NaN) returns: " 
                     + Math.Max(10, Double.NaN));
   Console.WriteLine("Math.Max(10, Double.NegativeInfinity) returns: " 
                     + Math.Max(10, Double.NegativeInfinity));
   Console.WriteLine("Math.Max(10, Double.PositiveInfinity) returns: " 
                     + Math.Max(10, Double.PositiveInfinity));
  }
}

The output of the above code will be:

Math.Max(50, 100) returns: 100
Math.Max(5.5f, 10.5f) returns: 10.5
Math.Max(5, 10.5f) returns: 10.5
Math.Max(10, Double.NaN) returns: NaN
Math.Max(10, Double.NegativeInfinity) returns: 10
Math.Max(10, Double.PositiveInfinity) returns: Infinity

❮ C# Math Methods