Check if variable null before assign to null?

后端 未结 8 1087
清歌不尽
清歌不尽 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:15

    This will make your code so much harder to read that even if it was an optimization it wouldn't be worth the trouble.

    And it's not an optimization. On most modern cpu's if statements are quite expensive.

提交回复
热议问题