Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding Dictionary<TKey, TValue> #31318

Closed
weitzhandler opened this issue Oct 28, 2019 · 7 comments
Closed

Allow overriding Dictionary<TKey, TValue> #31318

weitzhandler opened this issue Oct 28, 2019 · 7 comments

Comments

@weitzhandler
Copy link
Contributor

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

@weitzhandler weitzhandler changed the title Make Dictionary virtual Allow overriding Dictionary<TKey, TValue> by making its mutating methods virtual Oct 28, 2019
@weitzhandler weitzhandler changed the title Allow overriding Dictionary<TKey, TValue> by making its mutating methods virtual Allow overriding Dictionary<TKey, TValue> Oct 28, 2019
@stephentoub
Copy link
Member

stephentoub commented Oct 28, 2019

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.

@weitzhandler
Copy link
Contributor Author

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

@Clockwork-Muse
Copy link
Contributor

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

@weitzhandler
Copy link
Contributor Author

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.

@Clockwork-Muse
Copy link
Contributor

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

@weitzhandler
Copy link
Contributor Author

weitzhandler commented Oct 30, 2019

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.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@weitzhandler
Copy link
Contributor Author

weitzhandler commented May 10, 2020

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.

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants