Math.Min() Method in C#

The Math.Min() method in C# is used to return the smaller of two specified numbers. This method is overloaded to work with various numeric data types including byte, decimal, double, float, int, long, sbyte, short, uint, ulong, and ushort.

Syntax

Following are the most commonly used overloads −

public static int Min(int val1, int val2);
public static double Min(double val1, double val2);
public static decimal Min(decimal val1, decimal val2);
public static float Min(float val1, float val2);
public static long Min(long val1, long val2);
public static byte Min(byte val1, byte val2);
public static short Min(short val1, short val2);

Parameters

  • val1 − The first number to compare

  • val2 − The second number to compare

Return Value

Returns the smaller of the two values. If the values are equal, either value is returned. For floating-point numbers, if either value is NaN, then NaN is returned.

Using Math.Min() 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("Minimum Value from two byte values = " + Math.Min(val1, val2));
      Console.WriteLine("Minimum Value from two decimal values = " + Math.Min(val3, val4));
      Console.WriteLine("Minimum Value from two double values = " + Math.Min(val5, val6));
      Console.WriteLine("Minimum Value from two short values = " + Math.Min(val7, val8));
   }
}

The output of the above code is −

Minimum Value from two byte values = 10
Minimum Value from two decimal values = 1000
Minimum Value from two double values = 15.896745
Minimum Value from two short values = 20

Using Math.Min() with Integer and Float Values

Example

using System;
public class Demo {
   public static void Main(){
      int val1 = 10, val2 = 15;
      float val3 = 12.8f, val4 = 25.6f;
      Console.WriteLine("Minimum Value from two int values = " + Math.Min(val1, val2));
      Console.WriteLine("Minimum Value from two float values = " + Math.Min(val3, val4));
   }
}

The output of the above code is −

Minimum Value from two int values = 10
Minimum Value from two float values = 12.8

Special Cases with Floating-Point Numbers

Example

using System;
public class Demo {
   public static void Main(){
      double positiveInfinity = double.PositiveInfinity;
      double negativeInfinity = double.NegativeInfinity;
      double nan = double.NaN;
      double regularValue = 42.5;
      
      Console.WriteLine("Min(42.5, PositiveInfinity) = " + Math.Min(regularValue, positiveInfinity));
      Console.WriteLine("Min(42.5, NegativeInfinity) = " + Math.Min(regularValue, negativeInfinity));
      Console.WriteLine("Min(42.5, NaN) = " + Math.Min(regularValue, nan));
      Console.WriteLine("Min(0.0, -0.0) = " + Math.Min(0.0, -0.0));
   }
}

The output of the above code is −

Min(42.5, PositiveInfinity) = 42.5
Min(42.5, NegativeInfinity) = -Infinity
Min(42.5, NaN) = NaN
Min(0.0, -0.0) = -0

Conclusion

The Math.Min() method provides a simple way to find the smaller of two numeric values across various data types. It handles special floating-point cases like infinity and NaN appropriately, making it reliable for mathematical operations in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements