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#...
把它放在单独的功能中
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语法
正如海因齐在这个问题中的回答:
不幸的是,没有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)为什么不使用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)