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) +44System.Collections.Generic.Dictionary'2.Insert(TKey key, TValue value, Boolean add) +40System.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?
string getLabel(bool? value) { if (value == null) { ... } else if { ... } else { ... }; }- Julietbool? -> labelconverter, (2) alabel -> bool?converter, and (3) aSelectListfor aDropDownMenu(which I would have just passed the dictionary values to). In accordance with DRY, I wanted the labels in one place in case I later changed my mind to, say Y, N, NA. Since that wasn't allowed, I ended up going with three consts (one for each label) and a string array for theSelectList. Not as convenient, but good enough. - devuxerGuid?as key. You can see thebool?as the simplest available example. - ANeves