.Max()

Published Feb 15, 2023Updated Apr 29, 2023
Contribute to Docs

The Math.Max() method returns the greater 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

Codebyte Example

The following example is runnable and uses the Math.Max() method to return the greater number from the given two integer type numbers, number1 and number2:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C# on Codecademy