C#到VB.NET的转换:';Take(of T)';错误

本文关键字:of 错误 Take 转换 VB NET | 更新日期: 2023-09-27 18:01:15

C#代码

byte[] array = bytes.Take<byte>(num).ToArray<byte>();

VB.NET转换代码(我尝试了多个可转换代码。结果相同(

Dim array As Byte() = bytes.Take(Of Byte)(num).ToArray(Of Byte)()

(of Byte) 上出错

错误

Extension method 'Public Function Take(count As Integer) As System.Collections.Generic.IEnumerable(Of TSource)' defined in 'System.Linq.Enumerable' is not generic (or has no free type parameters) and so cannot have type arguments

C#到VB.NET的转换:';Take(of T)';错误

我对此没有太多经验,但如果VB编译器能够推断类型参数,它就不允许显式指定它们。假设bytesByte数组(或IEnumerable(Of Byte)(,您应该能够使用:

Dim array As Byte() = bytes.Take(num).ToArray()

坦率地说,在C#中冗余地指定类型参数也有点奇怪——我通常会写:

byte[] array = bytes.Take(num).ToArray();