Check for null in foreach loop

前端 未结 7 1684
小鲜肉
小鲜肉 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:25

    Using Null-conditional Operator and ForEach() which works faster than standard foreach loop.
    You have to cast the collection to List though.

       listOfItems?.ForEach(item => // ... );
    

提交回复
热议问题