Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Max() Method in C#
The Math.Max() method in C# returns the larger of two specified numbers. This static method is overloaded to work with various numeric types including int, double, decimal, float, and more.
Syntax
The Math.Max() method has multiple overloads for different numeric types −
public static int Max(int val1, int val2); public static double Max(double val1, double val2); public static decimal Max(decimal val1, decimal val2); public static float Max(float val1, float val2); public static long Max(long val1, long val2); public static byte Max(byte val1, byte val2); public static short Max(short val1, short val2); public static uint Max(uint val1, uint val2); public static ulong Max(ulong val1, ulong val2); public static ushort Max(ushort val1, ushort val2);
Parameters
val1: The first number to compare.
val2: The second number to compare.
Return Value
Returns the larger of the two values. If both values are equal, either value is returned. The return type matches the input parameter types.
Using Math.Max() with Different Data Types
Example
using System;
public class Demo {
public static void Main() {
byte val1 = 10, val2 = 15;
decimal val3 = 1000M, val4 = 1500M;
double val5 = 15.896745, val6 = 25.676843;
short val7 = 20, val8 = 30;
Console.WriteLine("Maximum Value from two byte values = " + Math.Max(val1, val2));
Console.WriteLine("Maximum Value from two decimal values = " + Math.Max(val3, val4));
Console.WriteLine("Maximum Value from two double values = " + Math.Max(val5, val6));
Console.WriteLine("Maximum Value from two short values = " + Math.Max(val7, val8));
}
}
The output of the above code is −
Maximum Value from two byte values = 15 Maximum Value from two decimal values = 1500 Maximum Value from two double values = 25.676843 Maximum Value from two short values = 30
Using Math.Max() with Integer and Float Types
Example
using System;
public class Demo {
public static void Main() {
int val1 = 10, val2 = 15;
float val3 = 10.8f, val4 = 25.6f;
Console.WriteLine("Maximum Value from two int values = " + Math.Max(val1, val2));
Console.WriteLine("Maximum Value from two float values = " + Math.Max(val3, val4));
// Using with equal values
Console.WriteLine("Maximum of equal values (5, 5) = " + Math.Max(5, 5));
// Using with negative numbers
Console.WriteLine("Maximum of negative values (-10, -3) = " + Math.Max(-10, -3));
}
}
The output of the above code is −
Maximum Value from two int values = 15 Maximum Value from two float values = 25.6 Maximum of equal values (5, 5) = 5 Maximum of negative values (-10, -3) = -3
Special Cases with Math.Max()
Example
using System;
public class Demo {
public static void Main() {
// Working with NaN values
double nan = Double.NaN;
double number = 10.5;
Console.WriteLine("Math.Max(NaN, 10.5) = " + Math.Max(nan, number));
Console.WriteLine("Math.Max(10.5, NaN) = " + Math.Max(number, nan));
// Working with infinity
double positiveInfinity = Double.PositiveInfinity;
double negativeInfinity = Double.NegativeInfinity;
Console.WriteLine("Math.Max(PositiveInfinity, 100) = " + Math.Max(positiveInfinity, 100));
Console.WriteLine("Math.Max(NegativeInfinity, -100) = " + Math.Max(negativeInfinity, -100));
}
}
The output of the above code is −
Math.Max(NaN, 10.5) = NaN Math.Max(10.5, NaN) = NaN Math.Max(PositiveInfinity, 100) = Infinity Math.Max(NegativeInfinity, -100) = -100
Key Rules
If either parameter is
NaN(Not a Number), the result isNaN.PositiveInfinityis considered greater than any finite number.NegativeInfinityis considered smaller than any finite number.Both parameters must be of the same numeric type, or implicit conversion must be possible.
Conclusion
The Math.Max() method provides a simple way to find the larger of two numeric values across different data types. It handles special cases like NaN and infinity values appropriately, making it reliable for mathematical operations in C# applications.
