Should variable declarations always be placed outside of a loop?

前端 未结 6 1404
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 15:39

Is it better to declare a variable used in a loop outside of the loop rather then inside? Sometimes I see examples where a variable is declared inside the loop. Does this ef

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 15:44

    I've generally preferred the latter as a matter of personal habit because, even if .NET is smart enough, other environments in which I might work later may not be smart enough. It could be nothing more than compiling down to an extra line of code inside the loop to re-initialize the variable, but it's still overhead.

    Even if they're identical for all measurable purposes in any given example, I would say the latter has less of a chance of causing problems in the long run.

提交回复
热议问题