gpt4 book ai didi

c# - C# 和 VB 之间的 Linq Select 编译方式不同

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:31 24 4
gpt4 key购买 nike

我有以下可正确编译的 C# 代码:

private string formatterCSharp(int number)
{
return "n" + number;
}

private void testInCSharp()
{
IEnumerable<int> list = new List<int>();

IEnumerable<string> formatted = list.Select(formatterCSharp);
}

如您所见,formatted 应该包含 list 的内容,每个内容都应用了 formatterCSharp

当我尝试在 VB 中复制这段代码时,我想到了:

Private Function formatterVisualBasic(ByVal number As Integer) As String
Return "n" + number
End Function

Private Sub testInVB()
Dim list As IEnumerable(Of Integer) = New List(Of Integer)

Dim formatted As IEnumerable(Of String) = list.Select(formatterVisualBasic)
End Sub

但是我在 VB 中的 Select 语句中遇到了两个编译错误。

BC30455
Argument not specified for parameter 'number' of 'Private Function formatterVisualBasic(number As Integer) As String'.

BC30518
Overload resolution failed because no accessible '[Select]' can be called with these arguments:
Extension method 'Public Function [Select](Of TResult)(selector As Func(Of Integer, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable': Type parameter 'TResult' cannot be inferred.
Extension method 'Public Function [Select](Of TResult)(selector As Func(Of Integer, Integer, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable': Type parameter 'TResult' cannot be inferred.

最佳答案

尝试

Dim formatted As IEnumerable(Of String) = list.Select(AddressOf formatterVisualBasic)

在 VB.net 中你不能指定函数名来传递它。

https://msdn.microsoft.com/en-us/library/y72ewk2b.aspx

关于c# - C# 和 VB 之间的 Linq Select 编译方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855687/

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