简体   繁体   English

C#覆盖字典ContainsKey

[英]C# override Dictionary ContainsKey

I just can't find any proper piece of code to do what i need. 我只是找不到任何合适的代码来满足我的需要。

Im using Dict.ContainsKey but due to the fact im always creating the Key i need to look for, i always get false for the ContainsKey (because hashKey is different and im creating the key i want to check all the time). 我正在使用Dict.ContainsKey但由于我总是创建我需要寻找的密钥的事实,因此对于ContainsKey我总是会得到false(因为hashKey是不同的并且我正在创建所有我想检查的密钥)。

can someone please advise how to override Contains key or how to handle keys comparing in this situation ? 有人可以建议在这种情况下如何重写包含密钥或如何处理密钥吗? My dictionary Looks like Dictionary<someObj, int> 我的字典看起来像Dictionary<someObj, int>

public class someObj
{
    public int someobjParam {get;set;}
    public int someobjParamTwo {get;set;}
}

You don't need to override ContainsKey - you need to either override Equals and GetHashCode in someObj (which should be renamed to conform to .NET naming conventions, btw) or you need to pass an IEqualityComparer<someObj> to the Dictionary<,> constructor . 你并不需要覆盖ContainsKey -你需要或者重载EqualsGetHashCodesomeObj (应改名以符合.NET命名约定,顺便说一句), 或者你需要传递一个IEqualityComparer<someObj>Dictionary<,>构造函数 Either way, that code is used to compare keys (and obtain hash codes from them). 无论哪种方式,该代码都用于比较键(并从中获取哈希码)。

Basically you need to make Equals determine equality, and make GetHashCode return the same code for equal objects and ideally different codes for different objects - see Eric Lippert's article on GetHashCode for more details. 基本上,您需要让Equals确定相等性,并使GetHashCode返回相同对象的相同代码,理想情况下返回不同对象的不同代码-有关更多详细信息,请参阅Eric Lippert在GetHashCode上的文章

Also, you should consider making someObj immutable: mutable dictionary keys are generally a bad idea, as if you modify the key in a hashcode-sensitive way after using it as a key within the dictionary, you won't be able to find it again. 另外,您应该考虑使someObj不可变:可变的字典键通常是一个坏主意,就像您在将其用作字典中的键之后以对哈希码敏感的方式修改键一样,您将无法再次找到它。 If the point of your custom type really is to be a key, then just make it immutable. 如果您的自定义类型的要点确实是关键,那么只需使其不可变即可。

For simplicity, you should also consider making someObj implement IEquatable<someObj> , and also think about whether it would be appropriate to be a struct instead of a class. 为简单起见,您还应该考虑使someObj实现IEquatable<someObj> ,并考虑将结构代替类是否合适。 If you implement IEquatable<someObj> you should also override object.Equals in a consistent way. 如果要实现IEquatable<someObj>应该覆盖object.Equals以一致的方式。 Usually the object.Equals implementation will just call the most strongly-typed IEquatable<T>.Equals implementation. 通常, object.Equals实现将只调用最强类型的IEquatable<T>.Equals实现。

You don't need to override ContainsKey , but rather instruct the dictionary when it should consider that two keys are equal. 您不需要重写ContainsKey ,而是在字典应认为两个键相等时指示字典。

One way to do that is by implementing IEquatable<someObj> in your key class. 一种方法是在您的键类中实现IEquatable<someObj> Do this if the concept of equality is global across your app: 如果平等的概念在您的应用程序中是全球性的,请执行以下操作:

public class someObj : IEquatable<someObj>
{
    public int someobjParam {get;set;}
    public int someobjParamTwo {get;set;}

    // override GetHashCode() and Equals(); for an example
    // see http://msdn.microsoft.com/en-us/library/ms131190%28v=vs.110%29.aspx
}

Another one is by implementing an IEqualityComparer<someObj> and passing it to the dictionary's constructor. 另一个是通过实现IEqualityComparer<someObj>并将其传递给字典的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM