GOTO是退出双Foreach的唯一途径吗?

ser*_*hio 1 .net c# vb.net goto

可能重复:
在vb.net中嵌套/退出

GOTO从双重的ForEach退出的唯一途径?

For Each city in cities
  For Each person in city.People
    If IsOK(person) GOTO Found_
  Next person
Next city

Found_: 
' ...
Run Code Online (Sandbox Code Playgroud)

问题是VB.NET,但也想知道C#...

Cha*_*ana 9

把它放在单独的功能中

  Private Function FindPerson(cities As List(of City)) As Person
     For Each city in cities
        For Each person in city.People
           If IsOK(person) Return person
        Next person
     Next city
     Return Nothing
  End Function
Run Code Online (Sandbox Code Playgroud)

和...

  Private Function ContainsPerson(cities As List(of City)) As Bool  
     For Each city in cities
        For Each person in city.People
           If IsOK(person) Return True
        Next person
     Next city
     Return False
  End Function
Run Code Online (Sandbox Code Playgroud)

编辑:修复VB语法

  • 我是唯一一个认为应该转换为LINQ函数的人吗? (4认同)

888*_*888 5

正如海因齐在这个问题中的回答:

不幸的是,没有exit two levels of for声明,但有一些解决方法可以避免Goto,这被认为是不好的做法:

  • 假外块

    Do
        For Each item In itemList
            For Each item1 In itemList1
                If item1.Text = "bla bla bla" Then
                    Exit Do
                End If
            Next
        Next
    Loop While False
    
    Run Code Online (Sandbox Code Playgroud)

    要么

    Try
        For Each item In itemlist
            For Each item1 In itemlist1
                If item1 = "bla bla bla" Then
                    Exit Try
                End If
            Next
        Next
    Finally
    End Try
    
    Run Code Online (Sandbox Code Playgroud)
  • 单独的功能:将循环放在一个单独的功能中,可以退出return.但是,这可能需要您传递大量参数,具体取决于您在循环中使用的局部变量数量.另一种方法是将块放入多行lambda中,因为这会在局部变量上创建一个闭包.

  • 布尔变量:这可能会使您的代码的可读性降低,具体取决于您拥有多少层嵌套循环:

    Dim done = False
    
    For Each item In itemList
        For Each item1 In itemList1
            If item1.Text = "bla bla bla" Then
                done = True
                Exit For
            End If
        Next
        If done Then Exit For
    Next
    
    Run Code Online (Sandbox Code Playgroud)


m-y*_*m-y 5

为什么不使用LINQ?

C#:

// Or use SingleOrDefault(...) if there can ever only be one.
var person = Cities.SelectMany(city => city.People)
                   .FirstOrDefault(person => IsOK(person));

if (person != null)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

VB.Net(我最好的尝试,我不是在其中详细说明):

// Or use SingleOrDefault(...) if there can ever only be one.
Dim person = Cities.SelectMany(Function(city) city.People)
                   .FirstOrDefault(Function(person) IsOK(person));

If person Not Nothing Then
    ...
End If
Run Code Online (Sandbox Code Playgroud)

如果你要做的就是查看是否有IsOK(person),那么请使用Any(...)扩展方法:

C#:

var isOK = Cities.SelectMany(city => city.People)
                 .Any(person => IsOK(person));
Run Code Online (Sandbox Code Playgroud)

VB.Net(我最好的尝试,我不是在其中详细说明):

Dim isOK = Cities.SelectMany(Function(city) city.People)
                 .Any(Function(person) IsOK(person));
Run Code Online (Sandbox Code Playgroud)