在 VB.NET 中添加对数组的函数引用

Dan*_*man 4 vb.net

有没有办法将函数的引用添加到 VB.NET 中的列表或数组?在 JavaScript 中是这样的:

function hello() {
console.log('hello, world!');
}

function test() {
console.log('test');
}

var functionList = [];

functionList.push(hello);
functionList.push(test);

functionList.forEach(function(n) {
n();
}
Run Code Online (Sandbox Code Playgroud)

sst*_*tan 5

当然。您可以创建一个Action委托列表:

Sub Hello()
    Console.WriteLine("hello, world!")
End Sub

Sub Test()
    Console.WriteLine("test")
End Sub

Sub Main()
    Dim functionList As List(Of Action) = New List(Of Action)()

    functionList.Add(AddressOf Hello)
    functionList.Add(AddressOf Test)

    For Each n As Action In functionList
        n()
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)