相关疑难解决方法(0)

在foreach循环中检查null

有没有更好的方法来执行以下操作:
我需要在继续循环之前检查file.Headers上的null

if (file.Headers != null)
{
  foreach (var h in file.Headers)
  {
   //set lots of properties & some other stuff
  }
}
Run Code Online (Sandbox Code Playgroud)

简而言之,由于我的代码中发生缩进的程度,因此将foreach写入if中看起来有点难看.

是值得评价的东西

foreach(var h in (file.Headers != null))
{
  //do stuff
}
Run Code Online (Sandbox Code Playgroud)

可能?

c# foreach null loops

79
推荐指数
6
解决办法
6万
查看次数

标签 统计

c# ×1

foreach ×1

loops ×1

null ×1