C# Array.Contains() 编译错误

发布于 2024-11-04 01:40:32 字数 436 浏览 0 评论 0原文

我试图在 C# 中使用 Array.Contains () 方法,但由于某种原因它无法编译,尽管我相信我正在使用 C# 4.0,并且 C# 应该在 3.0 及更高版本中支持此功能。

if (! args.Contains ("-m"))
    Console.WriteLine ("You must provide a message for this commit.");

我收到此错误:

Main.cs(42,15):错误CS1061:“System.Array”不包含“Contains”的定义,并且找不到接受“System.Array”类型的第一个参数的扩展方法“Contains” (您是否缺少 using 指令或程序集引用?)

我从命令行编译,没有选项:“csc Main.exe”。

I'm trying to use the Array.Contains () method in C#, and for some reason it's failing to compile, eve though I believe that I'm using C# 4.0, and C# should support this in 3.0 and later.

if (! args.Contains ("-m"))
    Console.WriteLine ("You must provide a message for this commit.");

And I get this error:

Main.cs(42,15): error CS1061: 'System.Array' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

I am compiling from the command line, with no options: "csc Main.exe".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

等风来 2024-11-11 01:40:32

您需要在程序开头添加using System.Linq;

You need to add using System.Linq; at the beginning of your program.

败给现实 2024-11-11 01:40:32

您忘记使用 System.Linq 了吗?

顺便说一句,如果您无法使用 LINQ,还有许多其他选项,例如 <代码>Array.Exists

Did you forget using System.Linq?

By the way, if you can't use LINQ there are many other options such as Array.Exists.

物价感观 2024-11-11 01:40:32

如果你不想使用 linq 尝试

((IList<string>)args).Contains("-m")

If you dont' want to use linq try

((IList<string>)args).Contains("-m")
猛虎独行 2024-11-11 01:40:32

答案说要包含 System.Linq 是正确的,但是此问题还有另一个原因。如果 .Contains() 的参数类型与数组的类型不匹配,您将收到此错误。

public class Product
{
    public long ProductID {get;set;}
}

public IEnumerable<Product> GetProductsByID(int[] prodIDs)
{
    using (var context = new MyDatabaseContext())
    {
        return context.Products.Where(product => prodIDs.Contains(product.ProductID)); // ['System.Array' does not contain a definition for 'Contains'] error because product.ProductID is long and prodIDs is an array of ints.
    }
}

The answers saying to include System.Linq are spot on, however there is one other cause of this problem. If the type of the argument for .Contains() does not match the type of the array, you will get this error.

public class Product
{
    public long ProductID {get;set;}
}

public IEnumerable<Product> GetProductsByID(int[] prodIDs)
{
    using (var context = new MyDatabaseContext())
    {
        return context.Products.Where(product => prodIDs.Contains(product.ProductID)); // ['System.Array' does not contain a definition for 'Contains'] error because product.ProductID is long and prodIDs is an array of ints.
    }
}
↙厌世 2024-11-11 01:40:32

我遇到了同样的问题,这

using System.Linq

是因为我试图将字符串与 int 进行比较,但不知何故它说

“System.Array”不包含“Contains”的定义

I had the same issue and I had

using System.Linq

It was because I was trying to compare string to int, but somehow it was saying

'System.Array' does not contain a definition for 'Contains'

残花月 2024-11-11 01:40:32

确保您使用正确版本的 CSC (csc /?) - 您需要 2010 版本才能编译 4.0。
您可能还需要添加其他库(/r 选项)才能成功编译。

Make sure you are using correct version of CSC (csc /?) - you need 2010 version to compile for 4.0.
You also may need to add additional libraries (/r option) for compile to succeed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文