Is if(items != null) superfluous before foreach(T item in items)?

后端 未结 12 1857
旧时难觅i
旧时难觅i 2020-12-07 14:28

I often come across code like the following:

if ( items != null)
{
   foreach(T item in items)
   {
        //...
   }
}

Basically, the

12条回答
  •  天命终不由人
    2020-12-07 15:02

    You could always test it out with a null list... but this is what I found on the msdn website

    foreach-statement:
        foreach   (   type   identifier   in   expression   )   embedded-statement 
    

    If expression has the value null, a System.NullReferenceException is thrown.

提交回复
热议问题