Open In App

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:
 

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:
 




// 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));
     
    }
}

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

 


Article Tags :
C#