C#
C# 在使用具名引數上非常方便, function 不需做任何修改即可使用
public double CalculateBMI(double height, double weight, bool isPrint = true)
{
double heightMeter = height / 100D;//double suffix
double bmi = weight / Math.Pow(heightMeter, 2);
//double bmi = Math.Floor(weight / Math.Pow(heightMeter, 2) * 100) / 100;
if (isPrint) Console.WriteLine("BMI=" + bmi);
return bmi;
}
傳入參數的方式使用類似 JSON 的表示法, 且傳入不需有順序性
CalculateBMI(height: 170, weight: 65);
CalculateBMI(height: 170, weight:...
6/17/2014
Variable-length Argument in Java, C#, Python
Java
public int sum(int... nums)
{
//nums.getClass().getTypeName() -> int[]
int total = 0;
for (int num : nums )
total += num;
return total;
}
C#
public int Sum(params int[] nums)
{
//nums.GetType() -> System.Int32[]
int total = 0;
for (int i = 0; i < nums.Length; i++)
total += nums[i];
return total;
}
Python
def sum(*nums):
#type(nums) -> <class 'tuple'>
total = 0
for num in nums:
total...
Subscribe to:
Posts (Atom)