Check for null in foreach loop

前端 未结 7 1685
小鲜肉
小鲜肉 2020-11-30 20:57

Is there a nicer way of doing the following:
I need a check for null to happen on file.Headers before proceeding with the loop

if (file.Headers != null         


        
7条回答
  •  难免孤独
    2020-11-30 21:29

    Frankly, I advise: just suck up the null test. A null test is just a brfalse or brfalse.s; everything else is going to involve much more work (tests, assignments, extra method calls, unnecessary GetEnumerator(), MoveNext(), Dispose() on the iterator, etc).

    An if test is simple, obvious, and efficient.

提交回复
热议问题