Dictionary.ContainsKey()-如何工作? [英] Dictionary.ContainsKey() - How does it work?

查看:336
本文介绍了Dictionary.ContainsKey()-如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关Dictionary.ContainsKey()工作原理的MSDN文档,但是我想知道它实际上是如何进行相等比较的?基本上,我有一个键为引用类型*的字典,并且我希望ContainsKey()方法检查该引用类型的某个属性作为确定键是否存在的基础.例如,如果我有一个Dictionary(MyObject, int)并且MyObject具有(属于int的)公共属性"TypeID",我可以让ContainsKey(MyObject myObject)检查其中一个键是否具有一个TypeID等于myObject?我可以重载==运算符吗?

I've read the MSDN documentation on how Dictionary.ContainsKey() works, but I was wondering how it actually makes the equality comparison? Basically, I have a dictionary keyed to a reference type* and I want the ContainsKey() method to check a certain property of that reference type as its basis for determining if the key exists or not. For example, if I had a Dictionary(MyObject, int) and MyObject has a public property (of int) called "TypeID", could I get ContainsKey(MyObject myObject) to check to see if one of the keys has a TypeID that is equal to myObject? Could I just overload the == operator?

  • 引用类型是一个名为"Duration"的对象,其中包含一个值(double Length); 持续时间"是我的音乐程序中使用的基本类型,用于表示特定声音持续多长时间.我从中衍生出一些类,这些类中包含了更复杂的计时概念,例如西方音乐的拍号,但希望它们在长度上都具有可比性.
  • The reference type is an object called "Duration" which holds a value (double Length); "Duration" is a base type used in my music program to denote how long a particular sound lasts. I derive classes from it which incorporate more sophisticated timing concepts, like Western musical time signatures, but want all of them to be comparable in terms of their length.

按照建议,我在对象上实现了IEquitable,就像这样:

As suggested, I implemented IEquitable on my object like so:

 public class Duration : IEquatable<Duration>
 {
    protected double _length;

    /// <summary>
    /// Gets or Sets the duration in Miliseconds.
    /// </summary>
    public virtual double Length
{
        get
        {
            return _length;
        }
        set
        {
            _length = value;
        }
    }

// removed all the other code that as it was irrelevant

    public override bool Equals(object obj)
    {
        Duration otherDuration = (Duration)obj;
        if (otherDuration._length == _length)
        {
            return true;
        }
        else
        {
            return false
        }
    }

}

这就是我需要做的吗?

推荐答案

这是您更新后的示例的代码.注意:我发现您将该字段公开为受保护对象,并且还具有一个公开该成员的虚拟属性,这有点奇怪.在此方案下,某些东西可能会覆盖Length,从而导致相等于_lenght的行为不符合预期.

here is code for your updated example. Note: I find it a little odd that you expose the field as protected, and also have a virtual property that exposes the member. Under this scheme something could override Length resulting in equality that looks at _lenght to not behave as expected.

public class Duration : IEquatable<Duration>
{
    protected double _length;

    /// <summary>
    /// Gets or Sets the duration in Miliseconds.
    /// </summary>
    public virtual double Length
    {
        get { return _length; }
        set { _length = value; }
    }

    // removed all the other code that as it was irrelevant

    public bool Equals(Duration other)
    {
        // First two lines are just optimizations
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;

        return _length.Equals(other._length);
    }

    public override bool Equals(object obj)
    {
        // Again just optimization
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;

        // Actually check the type, should not throw exception from Equals override
        if (obj.GetType() != this.GetType()) return false;

        // Call the implementation from IEquatable
        return Equals((Duration) obj);
    }

    public override int GetHashCode()
    {
        // Constant because equals tests mutable member.
        // This will give poor hash performance, but will prevent bugs.
        return 0;
    }
}


有关默认 EqualityComparer.Default ./msdn.microsoft.com/en-us/library/ms132151.aspx> IEqualityComparer 由Dictionary类使用.


See EqualityComparer.Default for information on the default IEqualityComparer used by the Dictionary class.

如果您不想在类上普遍覆盖GetHashCodeEquals,或者无法这样做. 字典构造函数的重载,您可以在其中提供特定的

If you do not want to generally override GetHashCode and Equals on the class, or if you are unable to. There is an overload of the Dictionary constructor in which you can provide the specific IEqualityComparer to use.

这是一个简单的实现接口,但是您需要注意遵守GetHashCode的合同,否则可能会导致意想不到的行为.

It is a simple interface to implement, but you do need to be careful that you respect the contract for GetHashCode or you can end up with unexpected behavior.

public class MyObjectEqualityComparer : IEqualityComparer<MyObject>
{
    public bool Equals(MyObject x, MyObject y)
    {
        return x.TypeID == y.TypeID;
    }

    public int GetHashCode(MyObject obj)
    {
        return obj.TypeID; //Already an int
    }
}

使用它就可以了

new Dictionary<MyObject, int>(new MyObjectEqualityComparer());   

如果要使用默认的IEqualityComparer,则需要在MyObjectEqualityComparer上提供大致相同的方法.如果实现,您 可以避免覆盖object.Equals() IEquatable .但是,我强烈建议不要这样做,因为这样做会产生一些令人惊讶的行为.最好改写Equals,以便对Equals的所有调用都具有一致的行为,并具有与Equals正确匹配的哈希.我不得不修复以前的开发人员仅实现IEquatable.

If you want to use the default IEqualityComparer you need to provide roughly the same methods on MyObjectEqualityComparer. You can avoid overriding object.Equals() if you implement IEquatable. However I would strongly discourage it because doing so can create some surprising behavior. You are better of overriding Equals so that you have consistent behavior for all calls to Equals and have hashing that properly matches Equals. I have had to fix a bug in inherited code caused by a past developer only implementing IEquatable.

这篇关于Dictionary.ContainsKey()-如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆