作为一个简单的例子,我可以说我有以下网格,我正在寻找特定的单元格值.找到后我不再需要处理循环.
foreach(DataGridViewRow row in grid.Rows)
{
foreach(DataGridViewCell cell in row.Cells)
{
if(cell.Value == myValue)
{
//Do Something useful
//break out of both foreach loops.
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是如何在C#中完成的.在Java中,我可以使用标签来命名最外层循环,然后打破该循环,但我似乎无法在C#中找到equivelant.
在c#中实现这一目标的最简洁方法是什么?我知道我可以设置一个布尔标志,并在外部循环中检查它以打破那个,但它似乎太冗长了.
谢谢,
Jim*_*mmy 66
1
foreach(DataGridViewRow row in grid.Rows)
foreach(DataGridView cell in row.Cells)
if (cell.Value == somevalue) {
// do stuff
goto End;
}
End:
// more stuff
Run Code Online (Sandbox Code Playgroud)
2
void Loop(grid) {
foreach(row in grid.Rows)
foreach(cell in row.Cells)
if (something) {
// do stuff
return;
}
}
Run Code Online (Sandbox Code Playgroud)
3
var cell = (from row in grid.Rows.OfType<DataGridViewRow>()
from cell in row.Cells.OfType<DataGridViewCell>()
where cell.Value == somevalue
select cell
).FirstOrDefault();
if (cell != null) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*ert 57
虽然上面的许多解决方案都是正确的并且回答了你的问题,但我会退后一步并问自己"还有另一种方法可以更清楚地表示程序的语义吗?"
我倾向于写这样的代码:
var query = from row in grid.Rows
from cell in row.Cells
where cell.Value == myValue
select cell;
if (query.Any())
{
// do something useful;
}
Run Code Online (Sandbox Code Playgroud)
为什么要写任何循环?您想知道特定集合是否具有特定成员,因此请编写一个询问该问题的查询,然后检查答案.
mqp*_*mqp 36
最愉快的方法是将第二个循环分解为一个函数,如下所示:
public void DoubleLoop()
{
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
if(whatever[i][j]) break; // let's make this a "double" break
}
}
}
Run Code Online (Sandbox Code Playgroud)
去
public bool CheckWhatever(int whateverIndex)
{
for(int j = 0; j < height; j++)
{
if(whatever[whateverIndex][j]) return false;
}
return true;
}
public void DoubleLoop()
{
for(int i = 0; i < width; i++)
{
if(!CheckWhatever(i)) break;
}
}
Run Code Online (Sandbox Code Playgroud)
当然,随意使用LINQ或其他任何东西来简化它(你也可以放入CheckWhatever
循环条件.)这只是原理的详细演示.
JB *_*ing 23
我只是将循环包装到一个函数中,并将函数返回作为退出循环的方法来解决我的问题.
Tim*_*ter 19
foreach (DataGridViewRow row in grid.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value == myValue)
{
goto EndOfLoop;
//Do Something useful
//break out of both foreach loops.
}
}
}
EndOfLoop: ;
Run Code Online (Sandbox Code Playgroud)
这将工作,但我建议使用布尔标志.
编辑:只是在这里添加一点警告; 一般认为使用goto是不好的做法,因为它们很快就会导致(几乎)无法维护的意大利面条代码.话虽这么说,它被包含在C#语言中,并且可以使用,所以显然有些人认为它具有有效的用法.知道该功能存在并非常谨慎使用.
Ecl*_*pse 14
为了完整起见,还有错误的方法:
try
{
foreach(DataGridViewRow row in grid.Rows)
foreach(DataGridViewCell cell in row.Cells)
if(cell.Value == myValue)
throw new FoundItemException(cell);
}
catch (FoundItemException ex)
{
//Do Something useful with ex.item
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*ebb 11
C#确实有一个goto语句.实际上,MSDN上的示例使用它来打破双嵌套循环.
最好的方法是不要这样做.认真; 如果你想在嵌套循环中找到第一个出现的东西,然后完成查找,那么你想要做的就是不要检查每个元素,这显然只是foreach构造的作用.我建议在循环不变量中使用带有终止标志的常规for循环.
这是 for 循环的另一个解决方案:
bool nextStep = true;
for (int x = 0; x < width && nextStep; x++) {
for (int y = 0; y < height && nextStep; y++) {
nextStep = IsBreakConditionFalse(x, y);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以编写一个在一般情况下实现IEnumerator <T>的类,然后您的枚举代码如下所示:
foreach (Foo foo in someClass.Items) {
foreach (Bar bar in foo.Items) {
foreach (Baz baz in bar.Items) {
yield return baz;
}
}
}
// and later in client code
MyEnumerator e = new MyEnumerator(someClass);
foreach (Baz baz in e) {
if (baz == myValue) {
// do something useful
break;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46959 次 |
最近记录: |