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

Feature Request: Add AddOrUpdate to the Dictionary API #1221

Closed
kgiszewski opened this issue Dec 30, 2019 · 8 comments
Closed

Feature Request: Add AddOrUpdate to the Dictionary API #1221

kgiszewski opened this issue Dec 30, 2019 · 8 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections

Comments

@kgiszewski
Copy link

There is a Add(TKey key, TValue value) method but it throws if using a key that is already in use.

Adding AddOrUpdate(TKey key, TValue value) will simply use an existing key or add it.

Simply to be more clear and avoid boilerplate code and possibly an unnecessary lookup.

PR coming soon as it's fairly trivial.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Collections untriaged New issue has not been triaged by the area owner labels Dec 30, 2019
@omariom
Copy link
Contributor

omariom commented Dec 30, 2019

Isn't it what the indexer does?

@kgiszewski
Copy link
Author

Maybe, do you have a link reference?

@kgiszewski
Copy link
Author

Ah yes, I suppose what I'm advocating is to avoid having to use that syntax. Instead the simple update would look something like this:

public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
  dictionary[key] = value;
}

//usage
var dictionary = new Dictionary<string, string>();

dictionary.Add("myKey", "someValue");//succeeds
dictionary.Add("myKey", "someValue");//duplicate key exception

dictionary.AddOrUpdate("myKey", "someValue");//succeeds
dictionary.AddOrUpdate("myKey", "someValue2");//succeeds

Note: that I'm using it as an extension method at present.

I'll hold on the PR for now.

@TonyValenti
Copy link

I second the request for AddOrUpdate. The Indexer does the same thing but you can't pass indexers as Funcs and such. I'd also request a GetOrAdd method similar to what ConcurrentDictiony has.

@jkotas
Copy link
Member

jkotas commented Dec 31, 2019

Related discussion: https://github.com/dotnet/corefx/issues/31126

@stephentoub stephentoub added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 7, 2020
@fowl2
Copy link

fowl2 commented Jan 23, 2020

To match the shape of ConcurrentDictionary the return type should be TValue.

@eiriktsarpalis
Copy link
Member

Related to #36942

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2020
@ghost ghost 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.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Collections
Projects
None yet
Development

No branches or pull requests

9 participants