为什么 Enumerable.Zip() 的 2 参数重载在 .NET Core 中而不是在 .NET Standard 中?

Why is the 2-argument overload of Enumerable.Zip() in .NET Core but not in .NET Standard?

我在我的 Visual Studio 类型 class 库、.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}");
            }
        }
    }
}

编译器毫无怨言地接受了。

现在我创建了一个类型为 class 库、.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 上的 "go to definition",我会看到在 .NET Core 项目可访问的 Enumerable 中有两个 Zip() 重载,但在.NET Standard 项目可枚举访问的只有一个。缺少 2 参数重载。 .NET Standard 版本还缺少少量其他方法:SkipLast()、TakeLast() 和 ToHashSet()。为什么这些方法,特别是 Zip() 的重载从 .NET Standard 中省略了?

.NET Standard 是 .NET Framework 和 .NET Core 都支持的通用库。

Enumerable.Zip在.NET Framework中只有一个重载,而实际上,第二个重载是在.NET Core 3.0中才引入的。

.NET Framework 不再由 Microsoft 主动更新,因此存在差异。

如果您希望它在 class 库、.NET Standard、C# 中工作,则您需要查找以下代码。您将需要定义结果选择器“Func<TFirst,TSecond,TResult> 指定如何合并两个序列中的元素的函数。”

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, (n, c) => (n, c)))
{
    Console.WriteLine($"{n}, {c}");
}