VB.NET 有匿名函数吗?

发布于 2024-07-15 01:09:34 字数 407 浏览 9 评论 0原文

根据我在 google 上找到的信息,VB.NET 只有单语句 lambda,而没有多语句匿名函数。 然而,我读到的所有文章都在谈论旧版本的 VB.NET,我找不到比 vs2008 beta 1 或 2 更新的内容。

所以问题是:我怎样才能在 VB.NET 中做到这一点?

C#代码:

private void HandleErrors( Action codeBlock ){
    try{
        codeBlock();
    }catch(Exception e){
        //log exception, etc
    }
}

HandleErrors(() => {
    var x = foo();
    x.DoStuff();
    etc
});

From what I can find on google, VB.NET only has one-statement lambdas, and not multi-statement anonymous functions. However, all the articles I read were talking about old versions of VB.NET, I couldn't find anything more recent than vs2008 beta 1 or 2.

So the question: How can I do this in VB.NET?

C# code:

private void HandleErrors( Action codeBlock ){
    try{
        codeBlock();
    }catch(Exception e){
        //log exception, etc
    }
}

HandleErrors(() => {
    var x = foo();
    x.DoStuff();
    etc
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

枕花眠 2024-07-22 01:09:34

它在 VB10 中是这样的:

Dim food = New With {
    .ID = 1,
    .Name = "Carrot",
    .Type = (
        Function(name As String)
            If String.IsNullOrEmpty(name) Then Return String.Empty

            Select Case name.ToLower()
                Case "apple", "tomato": Return "Fruit"
                Case "potato": Return "Vegetable"
            End Select

            Return "Meat"
        End Function
    )(.Name)
}
Dim type = food.Type

或者,对应于您的代码:

Sub HandleErrors(codeBlock As Action)
    Try
        codeBlock()
    Catch e As Exception
        ' log exception, etc.
    End Try
End Sub

HandleErrors(Sub()
        Dim x = foo()
        x.DoStuff()
        ' etc.
    End Sub)

It does in VB10:

Dim food = New With {
    .ID = 1,
    .Name = "Carrot",
    .Type = (
        Function(name As String)
            If String.IsNullOrEmpty(name) Then Return String.Empty

            Select Case name.ToLower()
                Case "apple", "tomato": Return "Fruit"
                Case "potato": Return "Vegetable"
            End Select

            Return "Meat"
        End Function
    )(.Name)
}
Dim type = food.Type

Or, corresponding to your code:

Sub HandleErrors(codeBlock As Action)
    Try
        codeBlock()
    Catch e As Exception
        ' log exception, etc.
    End Try
End Sub

HandleErrors(Sub()
        Dim x = foo()
        x.DoStuff()
        ' etc.
    End Sub)
森罗 2024-07-22 01:09:34

Visual Basic .NET 仅具有 lambda 表达式。

它在当前版本中不支持“匿名委托”,尽管在 VS2010 中它支持(并且在多行中)。

现在唯一的选择是在某处声明您的方法并使用 Addressof 运算符传递它。

Visual Basic .NET has only lambda expressions.

It does not support 'anonymous delegates" in the current version, though it will (and on multiple lines at that) in VS2010.

Right now the only option is to declare your method somewhere and pass it with the Addressof operator.

给我一枪 2024-07-22 01:09:34

VB9 只有单行匿名函数。 我们在 VB10 中添加完整的语句和多行 lambda。

VB9 has only single-line anonymous functions. We're adding full statement and multi-line lambdas in VB10.

墨落画卷 2024-07-22 01:09:34

在此示例中,我有一个操作列表,但只想从 ID 匹配的列表(T 个)中找到一个操作:

Return operations.Find(Function(p) p.OperationID = operationID)

操作 ID 是传递到方法的局部变量,操作是通用列表。

in this example I have a list of operations but only want to find one from a list (of T) where the IDs match:

Return operations.Find(Function(p) p.OperationID = operationID)

operationID is a local variable passed in to the method and operations is a generic list.

別甾虛僞 2024-07-22 01:09:34

匿名不是委托或函数
它是一种强大的动态类型,

您可以使用泛型函数

Sub Main()
      Dim PersonDynamicType = AnonyFunc(New With {.Name = "david", .Family = "Fisher"})
      Console.Write(PersonDynamicType.Name)
End Sub

Function AnonyFunc(Of t)(v As t) As t
      Return v
End Function

Anonymous isn't a delegate or function
it's a strong dynamical type

you can use generic functions

Sub Main()
      Dim PersonDynamicType = AnonyFunc(New With {.Name = "david", .Family = "Fisher"})
      Console.Write(PersonDynamicType.Name)
End Sub

Function AnonyFunc(Of t)(v As t) As t
      Return v
End Function
心清如水 2024-07-22 01:09:34

这是不准确的。 VB.NET 实际上有匿名方法。 这是一个例子:

Private Function JsonToObject(Of T)(Value As String) As T
    Dim JavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    Return JavaScriptSerializer.Deserialize(Of T)(Value)
End Function

Dim People As Generic.List(Of Person) = JsonToObject(Of Generic.List(Of Person))(Json)

This is inaccurate. VB.NET does in fact have anonymous methods. Here is an example:

Private Function JsonToObject(Of T)(Value As String) As T
    Dim JavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    Return JavaScriptSerializer.Deserialize(Of T)(Value)
End Function

Dim People As Generic.List(Of Person) = JsonToObject(Of Generic.List(Of Person))(Json)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文