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

后端 未结 6 1994
眼角桃花
眼角桃花 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 08:09

    In JS the memory allocation is whole eachtime, In C#, there is no such difference usually, but if the local variable is captured by an anonymous method like lambda expression it will matter.

提交回复
热议问题