Is it better to declare a variable inside or outside a loop?

后端 未结 6 1992
眼角桃花
眼角桃花 2020-12-01 07:55

Is better do:

variable1Type foo; 
variable2Type baa; 

foreach(var val in list) 
{
    foo = new Foo( ... ); 
    foo.x = FormatValue(val); 

    baa = new B         


        
6条回答
  •  眼角桃花
    2020-12-01 07:58

    It doesn't matter, it has no effect on performance whatsoever.

    but I really want know to do right way.

    Most will tell you inside-the-loop makes the most sense.

提交回复
热议问题