3

I am trying to inherit from Dictionary<K,V>, but my problem is, that when the Dictionary.Keys are being queried, I want to override them. (that is, the KeyCollection) of the dictionary.

How can that be solved?

thanks

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Himberjack
  • 5,682
  • 18
  • 71
  • 115

1 Answers1

7
  • Encapsulate Dictionary<K,V>

and

  • Implement IDictionary<K,V>.

or

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

Note:

  • IDictionary<K,V>.Keys has type ICollection<Key>

  • Dictionary<K,V>.Keys has type Dictionary<K, V>.KeyCollection but it's marked as sealed so you will not be able to inherit it, so option 1 seems to be more realistic.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 3
    I would **definitely** recommend your first option (encapsulate `Dictionary` and implement `IDictionary`) otherwise when you cast your dictionary to `IDictionary` you will find that your `Keys` property is ignored because of the use of the `new` keyword. – Justin Nov 21 '11 at 11:42