Check if variable null before assign to null?

后端 未结 8 1173
清歌不尽
清歌不尽 2021-01-05 16:55

Is variable assignment expensive compared to a null check? For example, is it worth checking that foo is not null before assigning it null?

if (foo != null)          


        
8条回答
  •  一向
    一向 (楼主)
    2021-01-05 17:17

    This is actually (very, very slightly) less efficient. Variable assignments are roughly equivalent to null checks, plus there's an extra branch possible. Not that it makes much difference.

    Or is this worrying about nothing?

    You got it.

提交回复
热议问题