c#基础补充
params修饰符
static double CalculateAverage(params double[] values)
{
double sum = 0;
if (values.Length == 0)
return sum;
for (int i = 0; i < values.Length; i++)
{
sum+=values[i];
}
return sum / values.Length;
}
// params,传入 逗号 分隔的列表
AddLogLine(CalculateAverage(1.0, 3.0).ToString());
double[] data = { 1.0, 5.0 };
AddLogLine(CalculateAverage(data).ToString());
checked
static int Add(int a,int b)
{
return a+b;
}
// 溢出
try
{
checked
{
byte sum = (byte)Add(100, 200);
}
}
catch (OverflowException ex)
{
// Arithmetic operation resulted in an overflow.
AddLogLine(ex.ToString());
}
扩展方法
static class Extentions2
{
public static int ReverseDigits(this int i)
{
char[] digits = i.ToString().ToCharArray();
Array.Reverse(digits);
return int.Parse(new String(digits));
}
}
// 扩展方法
AddLogLine(1234.ReverseDigits().ToString());