我正在尝试从一个使用语句中退出,同时仍然保持在包含的for循环中。例如:
for (int i = _from; i <= _to; i++)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
if (condition is true)
{
// I want to quit the using clause and
// go to line marked //x below
// using break or return drop me to line //y
// outside of the for loop.
}
}
} //x
}
//y
我尝试使用break语句退出循环,但它将我带到了//y处,而我想保持在//x处的for循环中以便继续处理。我知道可以通过抛出异常并使用catch语句来实现,但如果有更优雅的方法退出循环,我宁愿不执行这个相对较昂贵的操作。谢谢!
if (condition is false)
语句包装在using语句中。 - Murkaeus