请考虑以下代码段。
class MyClass
{
public void ListInts( params int[] inVals )
{
if (inVals != null && inVals.Length != 0)
for (int i = 0; i < inVals.Length; i++) // Process the array.
{
inVals[i] = inVals[i] * 10;
Console.WriteLine("{0}", inVals[i]); // Display new value.
}
}
}
是否可以简化inVals != null && inVals.Length != 0
?
答案 0 :(得分:2)
实际上,您无需检查inVals.Length != 0
条件:
class MyClass {
public void ListInts(params int[] inVals ) {
if (inVals == null)
return;
foreach(int item in inVals)
Console.WriteLine("{0}", item * 10);
}
}
答案 1 :(得分:0)
试试这个:
public void ListInts(params int[] inVals)
{
if (inVals!=null)
{
Array.ForEach(inVals, (x) => Console.WriteLine("{0}", 10*x));
}
}