覆盖Dictionary <k,v> .KeyCollection </k,v>

时间:2011-11-21 11:27:34

标签: c# .net dictionary override

我试图继承Dictionary<K,V>,但我的问题是,当查询Dictionary.Keys时,我想覆盖它们。 (即KeyCollection)字典。

如何解决?

感谢

1 个答案:

答案 0 :(得分:7)

  • 封装Dictionary<K,V>

  • 实施IDictionary<K,V>

public new YourType Keys
{
    get
    {
        return yourVersion;
    }
}

注意:

  • IDictionary<K,V>.Keys的类型为ICollection<Key>

  • Dictionary<K,V>.Keys的类型为Dictionary<K, V>.KeyCollection,但标记为sealed,因此您无法继承它,因此选项1似乎更加真实。