- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我认为这是我们遇到的一个常见问题。
class Person
{
public string place;
public string name;
public Person(string place, string name)
{
this.place = place;
this.name = name;
}
public bool Equals(Person other)
{
if (ReferenceEquals(null, other))
return false;
return name == other.name;
}
public override bool Equals(object obj)
{
return Equals(obj as Person);
}
public override int GetHashCode()
{
return name.GetHashCode();
}
public override string ToString()
{
return place + " - " + name;
}
}
假设我有这门课。我可以实现 KeyedCollection
像这样:
class Collection : KeyedCollection<string, Person>
{
protected override string GetKeyForItem(Person item)
{
return item.place;
}
}
这里的情况是默认Equals
基于name
的 Person
但就我而言,我正在创建自定义 collection
那将只有一个 Person
每place
.换句话说 place
将在 collection
中是唯一的.
Person p1 = new Person("Paris", "Paul");
Person p2 = new Person("Dubai", "Ali");
var collection = new Collection { p1, p2 };
var p3 = new Person("Paris", "Jean");
if (!collection.Contains(p3))
collection.Add(p3); // explosion
我明白这个问题。 Contains(Person)
过载是 Collection<T>.Contains(T)
的过载在 Add(Person)
时进行基于值的线性搜索确实将值添加到内部字典,这可能导致重复键异常。在这里, had equality 是基于 place
,这个问题就不会存在。
我可以想出一个解决方法:
class Collection : KeyedCollection<string, Person>
{
protected override string GetKeyForItem(Person item)
{
return item.place;
}
new public bool Contains(Person item)
{
return this.Contains(GetKeyForItem(item));
}
}
但这又意味着如果我做一个一般的
var p3 = new Person("Paris", "Jean");
bool b = collection.Contains(p3); //true
返回 true
, 但实际上 Jean
collection
中不存在然而。所以我的问题是,KeyedCollection<K, T>
只有在 Equals
时才有意义仅基于 K
T
的一部分?我的问题在语义方面很少。 我不是在寻求解决方案,只是知道是否对 KeyedCollection
有道理吗?我在文档中找不到与此主题相关的任何内容。
我已经找到这里提到的确切问题 http://bytes.com/topic/net/answers/633980-framework-bug-keyedcollection-t
提问者向 MS 提交错误报告的位置。引用他的话(日期为 2007 年 4 月 18 日):
I submitted this as a bug to Microsoft, and they have verified it andaccepted it. It's Issue ID 271542, and can be tracked here:
"We have reproduced this bug on WinXP pro SP2 and VSTS2005 SP1, andwe are sending this bug to the appropriate group within the VisualStudio Product Team for triage and resolution."
虽然我不认为这是一个错误,但这确实是一个烦恼。但只是想知道 MS 最初是如何将此视为错误的(预计,现在无法找到该页面)。 Imo,它只是考虑不周的继承模型。
最佳答案
询问集合似乎有两件事是有意义的:
是否包含住在巴黎的人,以及
它是否包含一个住在巴黎的人。
KeyedCollection<TKey, TItem> Class恰好提供了两种方法来提出这些问题:
住在巴黎的名叫让的人与不是同一个人名叫保罗,住在巴黎 ( Person.Equals )。但是如果有一个人住在巴黎,那么根据你的规则,不可能有另一个 住在巴黎的人。
所以基本上你必须在添加新人之前向集合询问正确的问题:
if (!collection.Contains(p3.place))
collection.Add(p3);
为方便起见,您可以在类中添加一个 TryAdd 方法,如果成功添加了 人,或者如果集合中已经有一个 人住在同一个地方:
public bool TryAdd(Person person)
{
if (Contains(GetKeyForItem(person)))
return false;
Add(person);
return true;
}
关于c# - KeyedCollection 是不是只有Key 判断里面的item 是否相等的意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983720/
判断置顶文章 is_sticky() 函数用来判断一篇文章是否为置顶文章。 用法 ?
判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。 下面是大多数编程语言中典型的判断结构的一般形式: 判断语句 C
我经常这样写: (if (nil? a-value) another-value a-value) 是否有更简单的功能可用,例如: (if-nil? a-value another-value) 最佳
MySQL IF 语句允许您根据表达式的某个条件或值结果来执行一组 SQL 语句。 要在 MySQL 中形成一个表达式,可以结合文字,变量,运算符,甚至函数来组合。表达式可以返回 TRUE,FA
也就是说,是否有一种工具可以自动显示给定语法的完整语言,包括突出歧义(如果有)? 最佳答案 BNF 风格的文法可能有一些特殊性,但总的来说,确定给定的上下文无关文法(例如 BNF)是否有歧义是不可能的
有没有办法确定像下面这样的 Axios 请求是否收到了答案并完成了? axios.get('/api') .then(response => this.data = response.data); 最
我想请大家禁用 Firebug 。如何确定自己安装了firebug?所以它是一个跨浏览器,并在 Chrome、Mozilla 和 IE8 + 中确定 最佳答案 两步: 如果 window.consol
我有一个看起来像这样的对象: var searchFilter = {_id: XXX, approved: true} 用于驱动 Meteor 集合搜索过滤器。然后,我有一对文本框,允许用户输入一系
我正在循环并向我的数据库中插入几百万条记录。性能是第一要务。 我想利用无状态 session ,但您可能知道它们不支持在更复杂的实体上级联对象。 是否有一种通用方法可以确定实体是否具有级联记录?如果是
我正在使用 pdfminer 解析一些 PDF 文件。图书馆。 我需要知道文档是否是扫描文档,扫描机将扫描图像放在顶部,将 OCR 提取的文本放在背景中。 有没有办法识别文本是否可见,因为 OCR 机
我正在寻找一种方法来找出当前为浏览器游戏 TribalWars 编写的脚本打开的页面。 URL 的设置非常相似,对于知道自己在做什么的人来说这应该很容易(我显然不知道)。 URL 如下所示: http
我在 C# 中使用包装的 C 库,需要将图像从该库转换为位图并返回,但没有复制像素缓冲区。 转换为位图很简单: Bitmap WrapAsBitmap(CImage image) { retu
有没有办法检查调用方法的Controller是否来自Area内的Controller? 例如,我有一个继承自 AuthorizeAttribute 的类,例如 public class CustomA
是否可以找到MySQL View 中某列所属的表名? 如果 View 构造为 CREATE VIEW alpha_view AS SELECT alpha.col1, alpha.col2,
如何判断 .Net 应用程序是作为桌面应用程序运行还是作为服务运行? 我们正在尝试使用 Fitnesse 测试我们的应用程序,它将应用程序作为服务加载,然后调用它。但是当一个模式错误框被按下时,它就会
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及
我试图计算出 iframe 内容的大小,以便调整 iframe 元素的大小以包含其内容。 如何确定 iFrame 是否已加载以及我是否可以可靠地测量它的内容尺寸。 注意:onload 事件不会执行,因
这个问题在这里已经有了答案: How to write portable code in c++? (12 个答案) 关闭 9 年前。 我正在尝试编写可以用任何现代版本的 g++ 编译的代码,但遇到
这个问题在这里已经有了答案: distinguish shared objects from position independent executables (2 个答案) 关闭 4 年前。 我有
我的目标是如果 dte 与当前时间相差不到 1 小时,则停止循环。是否有“ ruby 方式”来做到这一点? #THIS IS AN INFINITE LOOP, DONT RUN THIS dte=D
我是一名优秀的程序员,十分优秀!