Bru*_*son 4 c# loops visual-studio
编辑
我把它留在这里,即使它让我看起来很愚蠢,因为它是一个微妙的虫子,如果你在深夜工作而不注意它会咬你.感谢Visual Studio提供这样一个智能解析器.
基本上我错过了我有嵌套循环,所以continue这里的语句基本上没用,因为它继续foreach循环,而不是for循环.
原始问题
我正在搜索工作簿,寻找符合所有字符串搜索条件的工作表.在Visual Studio编辑器中,i++下划线为"无法访问的代码".
/// <summary>
/// Finds the first sheet that has cells that match all the criteria.
/// </summary>
/// <param name="wb"></param>
/// <param name="searches"></param>
/// <returns></returns>
public static ISheet FindSheet( this IWorkbook wb, params string[] searches )
{
if( null == wb || null == searches )
return null;
for( int i = 0; i < wb.NumberOfSheets; i++ )
{
var sheet = wb.GetSheetAt( i );
foreach( var search in searches )
{
var cell = sheet.FindCell( search );
if( null == cell )
continue;
}
return sheet;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我认为该continue语句在这里有一个明确的含义:"如果任何搜索条件返回一个null单元格,继续下一次迭代.否则,只返回在此迭代中找到的工作表."
没有持续声明的更正代码
/// <summary>
/// Finds the first sheet that has cells that match all the criteria.
/// </summary>
/// <param name="wb"></param>
/// <param name="searches"></param>
/// <returns></returns>
public static ISheet FindSheet( this IWorkbook wb, params string[] searches )
{
if( null == wb || null == searches )
return null;
for( int i = 0; i < wb.NumberOfSheets; i++ )
{
var sheet = wb.GetSheetAt( i );
if( searches.All( s => null != sheet.FindCell( s ) ) )
return sheet;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
在这里,你的for循环完全没用.在第一次迭代之后,无论如何都会返回.当然你的foreach循环有一个continue语句,但这只适用于foreach循环.这就是您的代码无法访问的原因.
因此,仅分析第一张纸.如果这是你想要的,你可以简单地删除for循环并在索引0处定位工作表,或者你需要重新安排你的循环.