gpt4 book ai didi

c# - CS1061、CS0117 : `System.Linq.Enumerable' does not contain a definition for `Zip'

转载 作者:太空狗 更新时间:2023-10-30 00:07:49 26 4
gpt4 key购买 nike

我在 Mac OSX 10.8 上使用 Unity3D、Mono、C#。我正在尝试使用 .Net Enumerable.Zip .但是复制粘贴 MSDN 示例会给我一个 cs0117 错误。

最小的不工作示例:

using UnityEngine;
using System.Collections;
using System.Linq;

public class Asteroids : MonoBehaviour {
void Start () {
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
}
}

错误信息:

error CS1061: Type int[]' does not contain a definition forZip' and no extension method Zip' of typeint[]' could be found (are you missing a using directive or an assembly reference?)

我尝试用“Enumerable.Zip”替换“numbers.Zip”,然后我得到了这个:

error CS0117: System.Linq.Enumerable' does not contain a definition forZip'

为什么会发生这些?

最佳答案

鉴于@SLaks 的回答,推出您自己的 Zip 很容易:

public static IEnumerable<TResult> Zip<TA, TB, TResult>(
this IEnumerable<TA> seqA, IEnumerable<TB> seqB, Func<TA, TB, TResult> func)
{
if (seqA == null) throw new ArgumentNullException("seqA");
if (seqB == null) throw new ArgumentNullException("seqB");

using (var iteratorA = seqA.GetEnumerator())
using (var iteratorB = seqB.GetEnumerator())
{
while (iteratorA.MoveNext() && iteratorB.MoveNext())
{
yield return func(iteratorA.Current, iteratorB.Current);
}
}
}

关于c# - CS1061、CS0117 : `System.Linq.Enumerable' does not contain a definition for `Zip' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16927845/

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