C# .Max()

cslylla's avatar
Published Feb 15, 2023Updated Apr 29, 2023
Contribute to Docs

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours