Dictionary.GetValueOrDefault#14521
Conversation
| { | ||
| public static class CollectionExtensions | ||
| { | ||
| public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) |
There was a problem hiding this comment.
Linq's extension methods throw ArgumentNullException when the source is null. I think we should do the same for these extension methods, throwing ArgumentNullException if the dictionary is null.
| public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) | ||
| { | ||
| if (dictionary.ContainsKey(key)) | ||
| return dictionary[key]; |
There was a problem hiding this comment.
This is going to lookup the key twice. Consider something like:
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;There was a problem hiding this comment.
Interface IDictionary does not have TryGetValue method
There was a problem hiding this comment.
IDictionary<TKey, TValue> has TryGetValue.
|
Dictionary in the coreclr repo will need to be updated as well. |
|
The APIs will need to be exposed in the contracts by adding them to src/System.Collections/ref/System.Collections.cs within There should also be tests added as part of this. The tests for the |
|
@justinvp I tried expose API to ref, but after that project can not be compiled. I do not know how to compile it with new contract. Can you help me to compile it? |
|
@AlexRadch this PR should be in coreclr. The Dictionary class in CoreFX isn't used for netcoreapp builds, it's only there for building UAP. Sorry for the confusion about this. In the future you can figure out what we're actually taking from corefx and what we're taking from coreclr by looking at the csproj and seeing what TargetGroup the file is included under. I'd suggest you just close this out and reopen it against the coreclr repo. Then, once that merges, you can open another PR in CoreFX adding the new methods to the ref file and adding tests. |
|
@ianhays, the CollectionExtensions part of the change should still go in CoreFX. Once the Dictionary change in dotnet/coreclr#8641 is merged and propagated to CoreFX, would it make sense to continue with this PR which would: add CollectionExtensions, include the Dictionary change for UAP, add the new APIs to the contracts, and add tests? |
Thanks @justinvp, I didn't know there was already a coreclr PR. You're correct on the order of operations.
It won't compile until you merge the coreclr PR and CoreFX is updated with a new version of mscorlib that contains your coreclr changes. |
| TValue value; | ||
| dictionary.TryGetValue(key, out value); | ||
| return value; | ||
| } |
There was a problem hiding this comment.
This can be implemented in terms of the other overload to prevent code duplication. Same for the IDictionary method.
There was a problem hiding this comment.
You already did it. I was talking about doing this:
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) => dictionary.GetValueOrDefault(key, default(TValue));
There was a problem hiding this comment.
I understand your suggestion after wrote my comment and I changed code to remove duplication.
| // This allows them to continue getting that behavior with minimal code delta. This is basically | ||
| // TryGetValue without the out param | ||
| internal TValue GetValueOrDefault(TKey key) | ||
| public TValue GetValueOrDefault(TKey key) |
There was a problem hiding this comment.
Same comments from the coreclr PR apply here as well.
ianhays
left a comment
There was a problem hiding this comment.
Are there tests for this? You should be able to use the same ones as TryGetValue.
I had a few comments, but it mostly looks good to me. Thanks Alex. We'll need to wait a few days to consume the coreclr changes after they merge.
|
Source changes LGTM once we have tests. |
|
@dotnet-bot test this (updated clr) |
|
Trying to trigger retest. |
|
@AlexRadch do you plan to finish the work? |
|
The PR seems to be abandoned for 1 month. Closing it for now. |
With dictionaries of class-types it is often desirable and expected to return null for a non-existent key.
Close https://github.com/dotnet/corefx/issues/3482