C# | Math.Min() Method

In C#, Min() is a Math class method which returns the smaller of the two specified numbers. This method always takes two arguments and it can be overloaded by changing the data type of the passed arguments as follows:

  • Math.Min(Byte, Byte): Returns the smaller of the two 8-bit unsigned integers.
  • Math.Min(Decimal, Decimal): Returns the smaller of the two decimal numbers.
  • Math.Min(Double, Double): Returns the smaller of the two double-precision floating-point numbers.
  • Math.Min(Int16, Int16): Returns the smaller of the two 16-bit signed integers. Here Int16 is short data type.
  • Math.Min(Int32, Int32): Returns the smaller of the two 32-bit signed integers. Here Int32 is int data type.
  • Math.Min(Int64, Int64): Returns the smaller of the two 64-bit signed integers. Here Int64 is long data type.
  • Math.Min(SByte, SByte): Returns the smaller of the two 8-bit signed integers.
  • Math.Min(single, single): Returns the smaller of the two single-precision floating-point numbers. Here single is float data type.
  • Math.Min(UInt16, UInt16): Returns the smaller of the two 16-bit unsigned integers. Here UInt16 is unsinged short(ushort) data type.
  • Math.Min(UInt32, UInt32): Returns the smaller of the two 32-bit unsigned integers. Here UInt32 is unsinged int(uint) data type.
  • Math.Min(UInt64, UInt64): Returns the smaller of the two 64-bit unsigned integers. Here UInt64 is unsinged long(ulong) data type.

Common Syntax for all above methods:

public static data_type Min(Data_type first_value, Data_type second_value)

Parameter:



These methods always accept the two parameters of the specified data type.

Return Type: Methods return the minimum of the two numbers which specified into the parameter list and return type is depends on the type of arguments passed.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Math.Min() method
using System;
class GFG {
  
// Main Method
static void Main()
{
      
    // byte data type
    byte b1 = 10, b2 = 15;
      
    // decimal data type
    decimal d1 = 1000M, d2 = 1568M;
      
    // double data type
    double db1 = 15.896745, db2 = 8.62644598;
      
      
    // Int16 data type
    short sh1 = -96, sh2 = 24;
  
    // Int32 data type
    int i1 = 26745236, i2 = 36725413;
      
      
    // Int64 data type
    long l1 = -2534234234234, l2 = -745837587527423;
      
    // sbyte data type
    sbyte sb1 = 52, sb2 = 120;
      
    // single data type
    float f1 = 8.0f, f2 = 78.78f;
      
    // UInt16 data type
    ushort us1 = 5346, us2 = 6437;
      
    // UInt32 data type
    uint un1 = 432344637, un2 = 64762738;
      
    // UInt64 data type
    ulong ul1 = 34234234, ul2 = 673286478326;
      
  
    // displaying result
    Console.WriteLine("Math.Min Method (Byte, Byte) = " + Math.Min(b1, b2));
    Console.WriteLine("Math.Min Method (Decimal, Decimal) = " + Math.Min(d1, d2));
    Console.WriteLine("Math.Min Method (Double, Double) = " + Math.Min(db1, db2));
    Console.WriteLine("Math.Min Method (Int16, Int16) = " + Math.Min(sh1, sh2));
    Console.WriteLine("Math.Min Method (Int32, Int32) = " + Math.Min(i1, i2));
    Console.WriteLine("Math.Min Method (Int64, lInt64) = " + Math.Min(l1, l2));
    Console.WriteLine("Math.Min Method (SByte, SByte) = " + Math.Min(sb1, sb2));
    Console.WriteLine("Math.Min Method (Single, Single) = " + Math.Min(f1, f2));
    Console.WriteLine("Math.Min Method (UInt16, UInt16) = " + Math.Min(us1, us2));
    Console.WriteLine("Math.Min Method (UInt32, UInt32) = " + Math.Min(un1, un2));
    Console.WriteLine("Math.Min Method (UInt64, UInt64) = " + Math.Min(ul1, ul2));
      
    }
}

chevron_right


Output:

Math.Min Method (Byte, Byte) = 10
Math.Min Method (Decimal, Decimal) = 1000
Math.Min Method (Double, Double) = 8.62644598
Math.Min Method (Int16, Int16) = -96
Math.Min Method (Int32, Int32) = 26745236
Math.Min Method (Int64, lInt64) = -745837587527423
Math.Min Method (SByte, SByte) = 52
Math.Min Method (Single, Single) = 8
Math.Min Method (UInt16, UInt16) = 5346
Math.Min Method (UInt32, UInt32) = 64762738
Math.Min Method (UInt64, UInt64) = 34234234


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.