C# Math Max() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Max(a, b) compares two numbers and returns the larger one. Math.Max(10, 20) is 20.

Think of two players with scores. You do not care who they are, you only want the higher score. Max works with two numbers at a time, so to find the biggest of many numbers you call it again and again, keeping the best so far. Its partner is Min(), which returns the smaller number.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Max(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Max is in every .NET version, with overloads for all the common number types.

Syntax

Pass two numbers of the same type. You get one of them back.

C#
int a = Math.Max(10, 20);        // 20
double b = Math.Max(-1.5, -2.5);  // -1.5
long c = Math.Max(5L, 3L);        // 5
CallWhat it does
Math.Max(10, 20)Returns 20.
Math.Max(-5, -9)Returns -5. With negatives, the one closer to zero is larger.
Math.Max(7, 2.0)Mixes int and double. The int is widened, so the double overload runs and returns 7.
Math.Max(double.NaN, 5)Returns NaN. If either input is NaN, the answer is NaN.

Both numbers should have the same type. If the types differ, C# widens the smaller type (for example int to double), so the answer comes back as the wider type. For three numbers, nest the calls: Math.Max(a, Math.Max(b, c)). When both inputs are equal, you simply get that value.

Library declarations
C#
public static int Max(int val1, int val2);
public static long Max(long val1, long val2);
public static float Max(float val1, float val2);
public static double Max(double val1, double val2);
public static decimal Max(decimal val1, decimal val2);
// also byte, sbyte, short, ushort, uint, ulong, nint, nuint overloads

Parameters

ParameterDescription
val1Required. The first number to compare.
val2Required. The second number to compare, of the same type as val1 (or a type it can widen to).

Return Value

Return ValueDescription
Same type as the inputsThe larger of val1 and val2. For double and float, the result is NaN if either input is NaN.

C# Examples

Example 1 — Pick the Larger Number

Call Max with positive numbers, negative numbers, equal numbers, and a mix of types.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Max(10, 20));
        Console.WriteLine(Math.Max(-5, -9));
        Console.WriteLine(Math.Max(3, 3));
        Console.WriteLine(Math.Max(7, 2.0));
    }
}

How It Works

  1. Two ints. Math.Max(10, 20) uses the int overload and returns 20.
  2. Negatives. On the number line, -5 is to the right of -9, so -5 is the larger number.
  3. Equal inputs. When both values are the same, Max returns that value, here 3.
  4. Mixed types. The int 7 is widened to 7.0, and the double overload runs. The answer prints as 7 because it is a whole number.

Example 2 — Find the Highest Score

Use Max inside a loop to find the biggest value in an array, and nest it for three numbers.

C#
using System;

class Program {
    static void Main() {
        int[] scores = { 72, 95, 88, 61 };
        int best = scores[0];

        foreach (int score in scores) {
            best = Math.Max(best, score);
        }
        Console.WriteLine(best);

        int a = 4, b = 19, c = 11;
        Console.WriteLine(Math.Max(a, Math.Max(b, c)));
    }
}

How It Works

  1. Start with the first. best begins as scores[0], which is 72. This is the best score seen so far.
  2. Compare each score. For every score, Math.Max(best, score) returns the larger of the two, and we keep it in best.
  3. The answer. After 72, 95, 88, and 61 are checked, best holds 95.
  4. Three numbers. Math.Max(b, c) is 19, then Math.Max(a, 19) is 19. Nesting the calls lets two-number Max handle any count.

Example 3 — NaN Spoils the Answer

See what Max returns when one input is NaN, and how it behaves with infinity and int.MaxValue.

C#
using System;

class Program {
    static void Main() {
        double nan = double.NaN;

        Console.WriteLine(Math.Max(nan, 5));
        Console.WriteLine(Math.Max(5, nan));
        Console.WriteLine(double.IsNaN(Math.Max(nan, 5)));

        Console.WriteLine(Math.Max(double.NegativeInfinity, -1000));
        Console.WriteLine(Math.Max(int.MaxValue, 0));
    }
}

How It Works

  1. NaN in first place. Math.Max(nan, 5) returns NaN. A normal comparison like nan > 5 would be False, but Max does not hide the bad value.
  2. Order does not matter. Math.Max(5, nan) is also NaN. Either input being NaN is enough.
  3. Detect it. double.IsNaN returns True for the result. Use this check when a loop may meet NaN.
  4. Infinity and int limits. Negative infinity is smaller than any number, so -1000 wins. int.MaxValue (2147483647) beats 0 as you would expect.

Frequently Asked Questions

It returns the larger of two numbers. Math.Max(10, 20) is 20. It works with int, long, double, decimal, and other number types.
Loop over the array and keep a running best with best = Math.Max(best, item). You can also use the LINQ method Max() on the array, which does the loop for you.
For double and float, it returns NaN. A NaN input always produces a NaN answer, so check double.IsNaN when your data might contain it.
No. Math is a static class, so you call Math.Max(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.
No, it takes exactly two. Nest the calls for three, as in Math.Max(a, Math.Max(b, c)). For longer lists, use a loop or the LINQ Max() method.

Next: MaxMagnitude()

Return whichever of two numbers is farther from zero, keeping its sign.

MaxMagnitude() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful