Apparently, you cannot use a null
for a key, even if your key is a nullable type.
This code:
var nullableBoolLabels = new System.Collections.Generic.Dictionary<bool?, string>
{
{ true, "Yes" },
{ false, "No" },
{ null, "(n/a)" }
};
...results in this exception:
Value cannot be null. Parameter name: key
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
[ArgumentNullException: Value cannot be null. Parameter name: key]
System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) +44
System.Collections.Generic.Dictionary'2.Insert(TKey key, TValue value, Boolean add) +40
System.Collections.Generic.Dictionary'2.Add(TKey key, TValue value) +13
Why would the .NET framework allow a nullable type for a key, but not allow a null value?