I often come across code like the following:
if ( items != null)
{
foreach(T item in items)
{
//...
}
}
Basically, the if
condition ensures that foreach
block will execute only if items
is not null. I'm wondering if the if
condition is really needed, or foreach
will handle the case if items == null
.
I mean, can I simply write
foreach(T item in items)
{
//...
}
without worrying about whether items
is null or not? Is the if
condition superfluous? Or this depends on the type of items
or maybe on T
as well?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…