Difference between declaring variables before or in loop?

前端 未结 25 2482
长发绾君心
长发绾君心 2020-11-22 02:37

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (q

25条回答
  •  一个人的身影
    2020-11-22 03:07

    In my opinion, b is the better structure. In a, the last value of intermediateResult sticks around after your loop is finished.

    Edit: This doesn't make a lot of difference with value types, but reference types can be somewhat weighty. Personally, I like variables to be dereferenced as soon as possible for cleanup, and b does that for you,

提交回复
热议问题