相关疑难解决方法(0)

Dictionary.ContainsKey() - 它是如何工作的?

我已经阅读了关于如何Dictionary.ContainsKey()工作的MSDN文档,但我想知道它实际上是如何进行相等比较的?基本上,我有一个键入引用类型*的字典,我希望该ContainsKey()方法检查该引用类型的某个属性作为确定密钥是否存在的基础.举例来说,如果我有一个Dictionary(MyObject, int)MyObject拥有(的公共财产int被称为"TYPEID"),我能得到ContainsKey(MyObject myObject)检查,看是否的关键之一具有TypeID等于myObject?我可以超载==操作员吗?

  • 引用类型是一个名为"Duration"的对象,它包含一个value(double Length); "持续时间"是我的音乐节目中使用的基本类型,表示特定声音持续多长时间.我从中派生出类,其中包含更复杂的时序概念,如西方音乐时间签名,但希望所有这些都在长度方面具有可比性.

编辑:正如所建议的,我在我的对象上实现了IEquitable,如下所示:

 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 …
Run Code Online (Sandbox Code Playgroud)

.net c# idictionary reference-type

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

idictionary ×1

reference-type ×1