在VB.NET中使用Yield时出错 - 方法参数必须括在括号中

sel*_*udy 1 .net vb.net ienumerable yield

我试图创建一个递归函数来返回一个控件的所有后代IEnmerable(Of Control).我创建了一个返回IEnumerable(Of Control)并使用的函数Yield:

Public Function GetControls(C As Control) As IEnumerable(Of Control)
    For Each Child As Control In C.Controls
        Yield Child
        For Each GrandChild In GetControls(Child)
            Yield GrandChild
        Next
    Next
End Function
Run Code Online (Sandbox Code Playgroud)

但我有一个编译时错误:

错误BC30800,方法参数必须括在括号中.

我尝试使用它像函数Yield(Child)Yield Return ChildReturn Yield Child但仍然有错误.

通过在google或bing中搜索错误消息,我找不到任何与此问题相关的内容.我该如何解决这个问题?

sel*_*udy 7

Yield在VB.NET中使用语句时,该函数应定义为Iterator:

Public Iterator Function GetControls(C As Control) As IEnumerable(Of Control)
    For Each Child As Control In C.Controls
        Yield Child
        For Each GrandChild In GetControls(Child)
            Yield GrandChild
        Next
    Next
End Function
Run Code Online (Sandbox Code Playgroud)