[英]C# Array.Contains () compilation error
我正在嘗試在 C# 中使用 Array.Contains () 方法,並且由於某種原因它無法編譯,盡管我相信我使用的是 C# 4.0,並且 ZD7EFA19FBE7D30972FDZ5ADB6 和更高版本應該支持這個。
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”。
您需要using System.Linq;
在您的程序開始時。
您忘記using System.Linq
嗎?
順便說一句,如果您不能使用 LINQ ,還有許多其他選項,例如Array.Exists
。
如果你不想使用 linq 試試
((IList<string>)args).Contains("-m")
我有同樣的問題,我有
using System.Linq
這是因為我試圖將 string 與 int進行比較,但不知何故它在說
“System.Array”不包含“包含”的定義
確保您使用的是正確版本的 CSC (csc /?) - 您需要 2010 版本才能編譯為 4.0。 您可能還需要添加其他庫(/r 選項)以使編譯成功。
包含 System.Linq 的答案是正確的,但是這個問題還有另一個原因。 如果 for.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.
}
}
我有同樣的錯誤。 由於上面的評論,我意識到該數組不能是二維數組。 雖然我不知道有什么辦法可以克服...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.