【发布时间】:2010-09-27 13:06:50
【问题描述】:
我应该在 for 循环内部还是外部实例化我的工作变量
例如
一)
bool b = default(bool);
for (int i = 0; i < MyCollection.Length; i++)
{
b = false;
foreach(object myObject in myObjectCollection)
{
if (object.Property == MyCollection[i].Property)
{
b = true;
break;
}
}
if (b)
{
DoSomethingWith(MyCollection[i]);
}
}
b)
for (int i = 0; i < MyCollection.Length; i++)
{
bool b = default(bool);
foreach(object myObject in myObjectCollection)
{
if (object.Property == MyCollection[i].Property)
{
b = true;
break;
}
}
if (b)
{
DoSomethingWith(MyCollection[i]);
}
}
编辑:似乎普遍认为在 IL 方面没有区别。但是为了可读性和范围的清晰......内部更好
【问题讨论】:
-
我以前在外面做,但 ReSharper 告诉我在里面做。我会对人们的反应感兴趣
-
"bool b = default(bool)" 很古怪!