我看到一些人的代码是这样的:
while (!(baseType == typeof(Object)))
{
....
baseType = baseType.BaseType;
if (baseType != null)
continue;
break;
} while (baseType != typeof(Object));什么是while(...) {...} while(...)语句?
下面的代码是等价的吗?
while (baseType != null && baseType != typeof(Object))
{
....
baseType = baseType.BaseType;
}发布于 2011-08-23 19:08:24
您在一行中有两个while语句。第二个可能以无限循环结束,因为第一个运行到第一个条件为真或baseType变为null。然后第二个循环开始:
while (baseType != typeof(Object));如果baseType没有被另一个线程改变,循环就不会终止。因为第一个循环检查相同的条件,所以第二个循环永远不会运行,除非baseType为null。
您的代码并不完全相同,因为第一个代码在baseType为null时中断循环,然后在无限循环中结束。我更喜欢你的代码,它清晰多了。尽量避免继续和中断。
https://stackoverflow.com/questions/7160045
复制相似问题