dotnet / dotNext

Next generation API for .NET
https://dotnet.github.io/dotNext/
MIT License
1.56k stars 119 forks source link

Adding TryGetValue extension method to IReadOnlyDictionary #190

Closed AlexGirardDev closed 9 months ago

AlexGirardDev commented 10 months ago

So i noticed that there isn't an extension method for TryGetValue on specfically IReadOnlyDictionary

there is a static method on the dictionary class here that exsists but a extension would be much nicer syntax.

Would you all be open to me submititng a PR to add this extension method?

sakno commented 10 months ago

Could you propose exact API? Or you want just to add this modifier to the method parameter?

AlexGirardDev commented 10 months ago

Just adding the this keyword

sakno commented 9 months ago

It can be problematic because of ambiguity with another extension method TryGetValue in the same class. The second method has this modifier but for IDictionary<K, V> type. Unfortunately, IDictionary<,> doesn't derive from IReadOnlyDictionary. Additing this to another overload causes compilation error for the following code:

Dictionary<int, string> dict = new();
dict.TryGetValue(42); // which one TryGetValue to call?