-
I found it weird to get an Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'key') when I pass null into a function called TryGetValue(TKey, TValue). I know it's stated clearly in the Docs that this will happen when pass null. However, I believe this should be changed and returned as false instead of throwing an exception. |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 2 replies
-
Modifying this behavior would be a breaking change. And considering that keys cannot be But you can always define a new extension method, eg. public static bool MyTryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict,
TKey key, [MaybeNullWhen(false)]out TValue value)
{
if (key == null)
{
value = default;
return false;
}
return dict.TryGetValue(key, out value);
} And just a side note: I needed to create an |
Beta Was this translation helpful? Give feedback.
-
Also, this would be a request to the runtime repository, which handles the base class libraries and APIs for .NET. |
Beta Was this translation helpful? Give feedback.
Modifying this behavior would be a breaking change. And considering that keys cannot be
null
this behavior makes sense.But you can always define a new extension method, eg.
And just a side note: I needed to create an
AllowNullDictionary
type once. But please note that itsTryGetValue
explicitly contains a[CanBeNull]
attribute for thekey
parameter, which triggered CS8767 (Nullability of re…