Himberjack
Himberjack

Reputation: 5802

Overriding of Dictionary<K,V>.KeyCollection

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

Upvotes: 3

Views: 225

Answers (1)

abatishchev
abatishchev

Reputation: 100288

  • 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.

Upvotes: 7

Related Questions