[英]How are KeyValuePairs compared for equality?
I have two arrays of KeyValuePair<string, object>
. 我有两个
KeyValuePair<string, object>
数组。 I am comparing the two arrays, but noticed that when the value of the KeyValuePair
is a value type cast to an object, like object{int}
, two pairs with identical keys and value values are not considered equal. 我正在比较两个数组,但是注意到,当
KeyValuePair
的值是KeyValuePair
转换为对象(如object{int}
)的值类型时,具有相同键和值的两对不视为相等。 However, when a reference type is cast to a value, like object{string}
, two pairs with identical key and value values are considered equal. 但是,当将引用类型转换为值时,例如
object{string}
,则将具有相同键值和值的两对视为相等。
I discovered this problem when I was using the .Except()
method on the two arrays, and noticed although they were identical only the pairs with string values were removed by the set difference. 当我在两个数组上使用
.Except()
方法时,我发现了这个问题,并注意到尽管它们是相同的,但是只有字符串值对通过设置差异被删除。 From what I've seen doing research on the equality comparer for KeyValuePair
, equality is by value, and not by reference. 从我看到的对
KeyValuePair
的相等比较器进行研究的结果来看,相等是按值而不是按引用。 Curiously, the reference for the pairs with string values should be different anyways. 奇怪的是,具有字符串值的对的引用无论如何都应该是不同的。
// array1 and array2 contain the same keys with identical values here
var array1 = GetOldItems(); // This returns a KeyValuePair<string, object>[]
var array2 = GetNewItems(); // This returns a KeyValuePair<string, object>[]
var diff = array1.Except(array2); // The difference is all pairs with non-string value
diff
ends up being all pairs in array1
with a non-string value, even though all pairs in array1
had identical keys and values to the pairs in array2
. 即使
array1
所有对具有与array2
的对相同的键和值, diff
最终仍是array1
中的所有对具有非字符串值。
EDIT: Here is the value of array1
and array2
as reported in the debugger: 编辑:这是在调试器中报告的
array1
和array2
的值:
{System.Collections.Generic.KeyValuePair<string, object>[5]}
[0]: {[Prop1, 1]}
[1]: {[Prop2, A]}
[2]: {[Prop2, B]}
[3]: {[Prop3, 3]}
[4]: {[Prop1, 2]}
Prop2
has keys where type of value is object{string}
and the other pairs have values with type of object{int}
. Prop2
具有键,其中值的类型为object{string}
,而其他键对具有类型为object{int}
。
All pairs for Prop1
and Prop3
remain in the set difference, while all pairs with Prop2
have been removed. Prop1
和Prop3
所有对保持设置的差异,而所有带有Prop2
对都已删除。
KeyValuePair
uses the default approach to struct equality. KeyValuePair
使用默认方法进行结构相等。 If two KeyValuePair<TKey, TValue>
have Key
properties that compare as equal for their type's default equality and Value
properties that compare as equal for their type's default equality, then they are equal. 如果两个
KeyValuePair<TKey, TValue>
Key
属性的类型的默认相等性相等,而Value
属性的类型的默认相等性相等,则它们相等。 Otherwise they are not. 否则他们不是。
I do not find what you report. 我找不到您报告的内容。 Eg:
例如:
new[] { new KeyValuePair<string, object>("a", 2) }.Except(
new[] { (new KeyValuePair<string, object>("a", 2))})
returns an empty sequence, because the contents of the two arrays are identical. 返回空序列,因为两个数组的内容相同。
I would wonder if perhaps you have pairs where the values are some reference type that does not override Equals()
and so are only considered equal if they are the same object. 我想知道您是否有对,其中值是某些引用类型,不会覆盖
Equals()
,因此仅当它们是同一对象时才被视为相等。 In other words, that kvp1.Value.Equals(kvp2.Value)
would return false. 换句话说,该
kvp1.Value.Equals(kvp2.Value)
将返回false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.