Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


The C# Max() method is used to return 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 below example, Max() method is used to find out the maximum value between the two arguments.

using System;

class MyProgram {
  static void Main(string[] args) {
   int x = 50, y = 100;
   float p = 5.5f, q = 10.5f;

   Console.WriteLine("Math.Max(" + x + "," + y + ") returns: " + Math.Max(x, y));
   Console.WriteLine("Math.Max(" + p + "," + q + ") returns: " + Math.Max(p, q));
  }
}

The output of the above code will be:

Math.Max(50,100) returns: 100
Math.Max(5.5,10.5) returns: 10.5

❮ C# Math Methods