Func(Of Tin,Tout)使用带有ByRef参数的lambda表达式会给出不兼容的签名错误 [英] Func(Of Tin, Tout) using a lambda expression with ByRef argument gives incompatible signature error
问题描述
为什么这样做:
Private [Function] As Func(Of Double, String) = Function(ByRef z As Double) z.ToString
出现以下错误:
嵌套函数没有与委托字符串兼容的签名)'。
Nested function does not have a signature that is compatible with delegate String)'.
此:
Private [Function] As Func(Of Double, String) = Function(ByVal z As Double) z.ToString
不是吗? (区别是ByRef / ByVal)
Does not? (The difference is ByRef/ByVal)
此外,我该如何实现这种东西?
Furthermore, how might I implement such a thing?
推荐答案
之所以会出现此错误,是因为委托类型函数(ByVal z作为字符串的Double)作为字符串与函数(ByRef z作为Double的字符串)作为String 。您需要完全匹配。
You are getting this error because the delegate type Function (ByVal z As Double) As String is not compatible with Function (ByRef z As Double) As String. You need exact match.
此外,您不能使用ByRef参数声明 Func(Of ...)泛型委托( ref 或 out 在C#中),无论您是否使用匿名函数。
Also you can't declare the Func(Of ...) generic delegate with ByRef parameters (ref or out in C#), no matter are you using anonymous function or not.
但是您可以声明您的委托类型然后甚至与您的匿名函数一起使用它
But you can declare you delegate type and then use it even with your anonymous function
Delegate Function ToStringDelegate(ByRef value As Double) As String
Sub Main()
Dim Del As ToStringDelegate = Function(ByRef value As Double) value.ToString()
End Sub
或您可以使用隐式键入(如果启用了Option Infer)
or you can use implicit typing (if the Option Infer is turned on)
Dim Del = Function(ByRef value As Double) value.ToString()
这篇关于Func(Of Tin,Tout)使用带有ByRef参数的lambda表达式会给出不兼容的签名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!