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

后端 未结 12 1884
旧时难觅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条回答
  •  旧时难觅i
    2020-12-07 14:52

    the second will throw a NullReferenceException with the message Object reference not set to an instance of an object.

提交回复
热议问题