C#,在for..loop中声明一个变量,会降低性能吗? [英] C#, Declaring a variable inside for..loop, will it decrease performance?
本文介绍了C#,在for..loop中声明一个变量,会降低性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
for (i = 0; i < 100; i++)
{
string myvar = "";
// Some logic
}
它会导致性能下降或内存泄漏吗?
Do it make performace or memory leak?
为什么要这样做,因为我不希望在for..loop之外访问"myvar".
Why i do this, because i don't want "myvar" accessible outside the for..loop.
它是任何性能监视器,我可以比较两个代码段或整个程序之间的执行时间吗?
It is any performance monitor, i can compare the execute time between two snippet or whole program ?
谢谢你.
推荐答案
不,变量纯粹是为了程序员的方便.声明它们的位置无关紧要.参见我的回答这个重复的问题以获取更多详细信息.
No, variables are purely for the programmer's convenience. It doesn't matter where you declare them. See my answer to this duplicate question for more details.
这篇关于C#,在for..loop中声明一个变量,会降低性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文