Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Math.Min() trong C#

Phương thức Math.Min() trong C# trả về giá trị nhỏ hơn trong hai số được chỉ định. Phương thức hỗ trợ nhiều kiểu: Double, Decimal, Int16, Int32, Byte, Long, UInt, Short, v.v.

Cú pháp (một số nạp chồng)

public static ulong Min(ulong val1, ulong val2);
public static uint Min(uint val1, uint val2);
public static ushort Min(ushort val1, ushort val2);
public static float Min(float val1, float val2);
public static byte Min(byte val1, byte val2);
public static long Min(long val1, long val2);

Ví dụ 1

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

Đầu ra

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

Ví dụ 2

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

Đầu ra

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