繁体   English   中英

CS1061,CS0117:`System.Linq.Enumerable'不包含`Zip'的定义

[英]CS1061, CS0117: `System.Linq.Enumerable' does not contain a definition for `Zip'

我在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);
}   
}

错误信息:

错误CS1061:类型int[]' does not contain a definition for Zip int[]' does not contain a definition for并且没有扩展方法可以找到Zip' of type int []'的Zip' of type (您是否缺少using指令或程序集引用?)

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

错误CS0117: System.Linq.Enumerable' does not contain a definition for Zip System.Linq.Enumerable' does not contain a definition for '

为什么这些发生了?

鉴于@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);
        }
    }
}

Zip()是.Net 4的新手。(不像LINQ的其余部分,它是在.Net 3.5中引入的)

看起来你的Mono版本没有它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM