如果我在foreach之一中遇到中断,如何退出Try块并返回。我能够使中断正常工作,但是它会进入下一个foreach:D哈哈:)它有趣的:p我应该尝试在try块内返回吗?请帮助纠正我的代码。
public void function()
{
Response() response= new Response();
try
{
1st Foreach ( if condition met, break; )
2nd Foreach ( if condition met, break; )
3rd Foreach ( if condition met, break; )
}
catch( Exception e)
{
}
return response;
}
答案 0 :(得分:0)
您必须修正Function
的返回类型才能返回Response
。是的,直接返回response
。
public Response Function()
{
var response = new Response();
try
{
foreach (...)
{
if (... /* condition met */)
return response;
}
foreach (...)
{
if (... /* condition met */)
return response;
}
foreach (...)
{
if (... /* condition met */)
return response;
}
}
catch (Exception ex)
{
}
return response;
}