【发布时间】:2014-09-09 06:23:12
【问题描述】:
我喜欢 pythons in-operator,我想编写它并在 c# 项目中使用它。 我只能访问 .NET Framework 2.0。我正在使用下面的代码,但我不明白编译错误。
using System;
using System.Collections.Generic;
using System.Text;
public static bool In<T>(this T item, IEnumerable<T> sequence) {
if (sequence == null)
throw new ArgumentNullException("sequence");
return sequence.Contains(item);
}
我也有对System 的引用,但我得到以下编译错误:
'System.Collections.Generic.IEnumerable<T>' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Collections.Generic.IEnumerable<T>' could be found
(are you missing a using directive or an assembly reference?)
我不明白我可能缺少什么参考资料!
编辑:
.NET 框架 2.0 中似乎没有 Linq。所以我下载并使用了以下包:http://www.albahari.com/nutshell/linqbridge.aspx 它包含一个namespace System.Linq,它解决了编译错误。
我想用下面的代码测试一下:
char[] x = { 'a', 'b', 'c' };
if('a'.In(x))
{
;
}
但我得到了令人困惑的错误:
'char' does not contain a definition for 'In' and no extension method 'In' accepting a first argument of type 'char' could be found
(are you missing a using directive or an assembly reference?)
编辑: 干净的构建解决了这些问题。
【问题讨论】:
-
Linq 在 .NET Framework 2.0 中不可用,因此您将无法使用“包含”
标签: c# python extension-methods operator-keyword