.Max()

The Math.Max() method returns the larger of two specified numbers.

Syntax

Math.Max(num1, num2)

Math.Max() returns the greater value from num1 and num2.

Example

The following example compares two integers i1 and i2 and writes the greater integer to the console.

using System;
public class Example {
public static void Main (string[] args) {
int i1 = -234, i2 = 345;
Console.WriteLine("Math.Max (int) = " + Math.Max(i1,i2));
}
}

This example results in the following output:

Math.Max (int) = 345

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn C# on Codecademy