我在我的Visual Studio中创建了类型类库,.NET Core和C#的新项目,并粘贴了以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyLibrary
{
public class Class1
{
public void Method()
{
var numbers = new List<int> { 1, 2, 3 };
var chars = new List<char> { 'a', 'b', 'c' };
foreach (var (n, c) in Enumerable.Zip(numbers, chars))
{
Console.WriteLine($"{n}, {c}");
}
}
}
}
编译器不加抱怨地接受这一点。
现在,我创建一个类库,.NET Standard,C#类型的新项目,并粘贴相同的代码并更改名称空间。编译器现在出现以下错误:
1>[path]\Class1.cs(15,47,15,50): error CS7036: There is no argument given that corresponds to the required formal parameter 'resultSelector' of 'Enumerable.Zip<TFirst, TSecond, TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst, TSecond, TResult>)'
1>[path]\Class1.cs(15,36,15,66): error CS1061: 'TResult' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'TResult' could be found (are you missing a using directive or an assembly reference?)
1>[path]\Class1.cs(15,36,15,66): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'TResult', with 2 out parameters and a void return type.
1>[path]\Class1.cs(15,27,15,28): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'n'.
1>[path]\Class1.cs(15,30,15,31): error CS8130: Cannot infer the type of implicitly-typed deconstruction variable 'c'.
如果我在每个代码副本中对Enumerable.Zip调用“转到定义”,我会看到在.NET Core项目可访问的Enumerable中,有两个Zip()重载,但在Enumerable中可访问。 NET Standard项目只有一个。 2参数重载丢失。 .NET标准版本中还缺少一些其他方法:SkipLast(),TakeLast()和ToHashSet()。为什么这些方法,特别是.NET Standard中省略了Zip()的重载?
。NET标准是.NET Framework和.NET Core都支持的通用库。
Enumerable.Zip
在.NET Framework中只有一个重载,而实际上,第二个重载仅是在.NET Core 3.0中引入的。
。NET Framework不再由Microsoft主动更新,因此有所不同。