C# 中的 Math.Min() 方法


C# 中的 Math.Min() 方法用于返回两个指定数字中较大的一个。此方法适用于数字为 Double、Decimal、Int16、Int32 等的情况。

语法

以下为语法 −

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

示例

现在让我们看一个实现 Math.Min() 方法的示例 −

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

输出

将产生以下输出 −

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

示例

让我们看另一个实现 Math.Min() 方法的示例 −

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

输出

将产生以下输出 −

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

更新于: 06-11-2019

2K+ 次浏览

开启你的 职业生涯

完成课程获取认证

开始学习
广告