C# Array.Contains() 编译错误
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要在程序开头添加
using System.Linq;
。You need to add
using System.Linq;
at the beginning of your program.您忘记
使用 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
.如果你不想使用 linq 尝试
If you dont' want to use linq try
答案说要包含 System.Linq 是正确的,但是此问题还有另一个原因。如果 .Contains() 的参数类型与数组的类型不匹配,您将收到此错误。
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.
我遇到了同样的问题,这
是因为我试图将字符串与 int 进行比较,但不知何故它说
I had the same issue and I had
It was because I was trying to compare string to int, but somehow it was saying
确保您使用正确版本的 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.