【问题标题】:List.Exists missing within Portable Class Library可移植类库中缺少 List.Exists
【发布时间】:2013-06-13 14:20:17
【问题描述】:

我正在创建一个可移植类库 (PCL) 并尝试使用 List.Exists() 和 List.TrueForAll(),但有人告诉我 System.Collections.Generic.List 不包含Exists 或 TrueForAll。我正在创建的 PCL 可以跨 .Net 4.5、Silverlight 4、Windows Phone 7.5、Mono Android 和 Mono iOS 工作。有什么我遗漏的吗?

注意:此代码适用于我制作的 .Net 4.0 库。

返回错误的代码示例:

List<object> set0;
List<object> set1;

if (set0.TrueForAll(obj0 => set1.Exists(obj1 => obj0.Equals(obj1))))
    return true;

if(!(set0.Exists(obj0 => !set1.Exists(obj1 => obj0.Equals(obj1)))))
    return true;

收到的错误:

错误:“System.Collections.Generic.List”不包含 'Exists' 的定义并且没有扩展方法 'Exists' 接受 'System.Collections.Generic.List' 类型的第一个参数 可以找到(您是否缺少 using 指令或程序集 参考?)

错误:“System.Collections.Generic.List”不包含 'TrueForAll' 的定义并且没有扩展方法'TrueForAll' 接受类型的第一个参数 'System.Collections.Generic.List' 可以找到(你是 缺少 using 指令或程序集引用?)

【问题讨论】:

  • 它们应该很容易被 Linq 的 All 和 Any 替换
  • This documentation 声称(在版本信息下)该方法支持:可移植类库
  • 我找到了这份文档,其中包含可以访问PLIB APIs 的库。我对这个列表完全不了解吗?

标签: c# .net-4.5 xamarin portable-class-library


【解决方案1】:

您似乎试图以一种麻烦的方式确定set0 是否是set1 的子集(数学上)。如果您将类型从List&lt;&gt; 更改为HashSet&lt;&gt;SortedSet&lt;&gt;,您将免费获得此功能。

否则考虑使用

set0.Except(set1).Any()

来自 Linq。

我不确定可移植类库 (PCL) 中存在哪些方法,但根据the List&lt;&gt;.Exists doc,此方法确实存在。还有我提到的 Linq 方法。

【讨论】:

  • 我无法访问此 PCL 中的 HashSet 或 SortedSet。我的目标框架是 .Net Framework 4.5、Silverlight 4 及更高版本、Window Phone 7.5、.Net for Windows Store Apps、Mono for Android 和 MonoTouch,这是否存在问题?
  • @BlazePhoenix 你有 Linq 吗?尝试using System.Linq; 然后.Any 等等。至少,像set1.Exists(obj1 =&gt; obj0.Equals(obj1)) 这样的东西应该改成set1.Contains(obj0)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
相关资源
最近更新 更多