是否有更好(更好)的方式来编写这个if语句?
Array a =null, b= null, c= null, d= null;
if(a!=null && b!=null && c!=null && d!=null)
{ //// TODO }
Run Code Online (Sandbox Code Playgroud)
您可以创建一个adhoc集合,然后对其执行Linq All查询:
if(!(new [] {a, b, c, d}).Any(_ => _ == null)) {
Console.WriteLine("None of the arrays are null");
}
Run Code Online (Sandbox Code Playgroud)
编辑:我刚刚看到你的评论.看起来你想在很多不同大小的地方使用它,所以你可以这样做:
static bool areNoneNull(params object[] things) {
return !things.Any(_ => _ == null);
}
public static void Main(String[] args) {
if(areNoneNull(a, b, c, d)) {
/* Do stuff */
}
if(areNoneNull(x, y)) {
/* Do different stuff */
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |