【问题标题】:No Overload for method 'Contains' takes 2 arguments with list and simple string方法“包含”的无重载采用列表和简单字符串的 2 个参数
【发布时间】:2020-06-15 06:08:00
【问题描述】:

借用example here,我尝试做以下事情:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
bool testCase = animals.Contains("horse", StringComparer.CurrentCultureIgnoreCase);

但只是这样做,我得到“方法 'Contains' 没有重载需要 2 个参数”。

我也试过这样:

List<string> animals = new List<string> { "Horse", "Cat", "Dog" };
string testAnimal = "horse";
bool testCase = animals.Contains(testAnimal, StringComparer.CurrentCultureIgnoreCase);
testCase = animals.Contains((string)testAnimal, StringComparer.CurrentCultureIgnoreCase);

但是这两个都得到相同的错误。

我在这里错过了什么?

【问题讨论】:

标签: c# list contains


【解决方案1】:

请注意,重载方法仅适用于目标 Win 10 版本 1809 及更高版本。

【讨论】:

    【解决方案2】:

    您必须使用扩展方法 Contains,它是命名空间 System.Linq 的一部分。 只需在文件顶部添加这一行,带有两个参数的方法应该可用。

    using System.Linq; 
    

    【讨论】:

      【解决方案3】:

      您可能正在搜索 Linq 扩展方法 Contains&lt;TSource&gt;(IEnumerable&lt;TSource&gt;, TSource, IEqualityComparer&lt;TSource&gt;),它记录在 here 中。

      在您的情况下,只需将using System.Linq; 添加到源文件的顶部并弹出该方法就足够了。

      【讨论】:

      • 我的系统没有 linq 命名空间...我正在按照 https://stackoverflow.com/questions/37694861/the-type-or-namespace-name-linq 中的步骤进行操作-does-not-exist-in-the-namespace-system 我什至没有看到 system.core 或 system.data.linq
      • 我们开始了...不知何故我的目标框架设置为 2.0 ... 是的...
      • 除非您使用的是非常旧的 .net Framework 版本(
      猜你喜欢
      • 2013-11-12
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2011-10-29
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      相关资源
      最近更新 更多