EDIT: Real answer to the question actually being asked: Why can't you use null as a key for a Dictionary?
The reason the generic dictionary doesn't support null is because TKey
might be a value type, which doesn't have null.
new Dictionary[null] = "Null"; //error!
To get one that does, you could either use the non-generic Hashtable
(which uses object keys and values), or roll your own with DictionaryBase
.
Edit: just to clarify why null is illegal in this case, consider this generic method:
bool IsNull (T value) {
return value == null;
}
But what happens when you call IsNull(null)
?
Argument '1': cannot convert from '' to 'int'
You get a compiler error, since you can't convert null
to an int
. We can fix it, by saying that we only want nullable types:
bool IsNull (T value) where T : class {
return value == null;
}
And, that's A-Okay. The restriction is that we can no longer call IsNull
, since int
is not a class (nullable object)