C# 中的 Math.Max() 方法
C# 中的 Math.Max() 方法用于返回两个指定数字中较大的数字。该方法适用于两个数字都是 Double、Decimal、Int16、Int32 等的情况。
语法
以下是语法 −
public static ulong Max (ulong val1, ulong val2); public static uint Max (uint val1, uint val2); public static ushort Max (ushort val1, ushort val2); public static float Max (float val1, float val2); public static byte Max (byte val1, byte val2); public static long Max (long val1, long val2);
示例
现在让我们看一个实现 Math.Max() 方法的示例 −
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("Maximum Value from two byte values = "+Math.Max(val1, val2)); Console.WriteLine("Maximum Value from two decimal values = "+Math.Max(val3, val4)); Console.WriteLine("Maximum Value from two double values = "+Math.Max(val5, val6)); Console.WriteLine("Maximum Value from two short values = "+Math.Max(val7, val8)); } }
输出
将产生以下输出 −
Maximum Value from two byte values = 15 Maximum Value from two decimal values = 1500 Maximum Value from two double values = 25.676843 Maximum Value from two short values = 30
示例
让我们看另一个实现 Math.Max() 方法的示例 −
using System; public class Demo { public static void Main(){ int val1 = 10, val2 = 15; float val3 = 10.8f, val4 = 25.6f; Console.WriteLine("Maximum Value from two int values = "+Math.Max(val1, val2)); Console.WriteLine("Maximum Value from two float values = "+Math.Max(val3, val4)); } }
输出
将产生以下输出 −
Maximum Value from two int values = 15 Maximum Value from two float values = 25.6
广告