gpt4 book ai didi

c# - 用作参数时无法推断 Action 委托(delegate)的返回类型

转载 作者:行者123 更新时间:2023-11-30 17:00:19 25 4
gpt4 key购买 nike

我在 VB.NET 中有以下方法:

Private Sub DelegateTest(ByVal doSomething As Action(Of TrimEntry))
doSomething(New TrimEntry())
End Sub

以下调用该方法的尝试导致编译时错误无法推断返回类型。考虑添加“As”子句以指定返回类型。。 (为简洁起见,我省略了一些代码,但我的调用需要的不仅仅是一行语句。)

DelegateTest(Function(item)
item.Length = 0
End Function)

也许我被 C# 宠坏了,但在 C# 中它非常简单。

DelegateTest(item => { item.Length = 0; })

问题:为什么会出现这个错误?我很困惑,因为类型是 Action 委托(delegate),它返回 void。为什么我会收到有关返回类型的错误?

最佳答案

由于您的匿名方法没有返回值,因此您需要使用 Sub 而不是 Function

关于c# - 用作参数时无法推断 Action 委托(delegate)的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22410027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com