Runtime: Allow overriding Dictionary<TKey, TValue>

Created on 28 Oct 2019  路  7Comments  路  Source: dotnet/runtime

Hi,

I always bump into this need to subclass a dictionary and be able to override its mutation methods, like Collection<T>.
There is KeyedCollection<TKet, TValue> but that only works if the key is exposed via the value.

As I'm sure there are, I tried searching for similar proposals but couldn't find any.

My request is that you add virtual SetItem, RemoveItem and ClearItems methods to Dictionary<TKey, TValue>'s, or offer a similar functionality in System.Collections.ObjectModel.

Alternatively, at least provide methods that get notified when the dictionary change, such as OnItemSet etc.

The problem is also with IDictionary<TKey, TValue> being a very large and verbose interface requiring to implement too many methods, sometimes to solve a very simple task.

Thanks

area-System.Collections

Most helpful comment

When the generic collections like List<> and Dictionary<> were introduced, virtuals were explicitly by design omitted. They can add a huge amount of overhead for these core collections types. I do not see us adding such virtual methods now.

All 7 comments

When the generic collections like List<> and Dictionary<> were introduced, virtuals were explicitly by design omitted. They can add a huge amount of overhead for these core collections types. I do not see us adding such virtual methods now.

Then please create another dictionary elsewhere for easy overriding, like Collection<T>.

_Why_ do you want to override the mutation methods? What problem are you actually trying to solve?

I'd be fine with just an ObservableDictionary, but I often find myself needing to expose a specific data type as dictionary, and set its values internally in a different manner.

but I often find myself needing to expose a specific data type as dictionary

Implement IDictionary yourself? It sounds like you need the interface, since

set its values internally in a different manner

... sounds like it's going to have some sort of "different" behavior (how, you haven't explained, so I'm not sure why you think you need something different yet).

I meant that I need both, although I need the notification more often.
And as I said, IDictionary has a lot of tedious implementation methods.
A DictionaryBase like Collection<T> would give all I need.

Based on the comments above, and based on my understanding that this is something of low demand, I'm gonna go ahead and gonna close this issue.

Was this page helpful?
0 / 5 - 0 ratings