69

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?

devuxer
  • 41,681
  • 47
  • 180
  • 292
  • Why exactly would you need a dictionary for this? A switch statement accomplishes the same job: `string getLabel(bool? value) { if (value == null) { ... } else if { ... } else { ... }; }` – Juliet Feb 01 '10 at 04:58
  • 3
    @Juliet, long story, but I'll try: I was going to be using the three labels (yes, no, n/a) in three different places: (1) a `bool? -> label` converter, (2) a `label -> bool?` converter, and (3) a `SelectList` for a `DropDownMenu` (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 the `SelectList`. Not as convenient, but good enough. – devuxer Feb 01 '10 at 05:23
  • Sometimes you have to go back to C++ to see the logic behind the implementations in .NET.. Have a look at my answer below. – Mahmoud Al-Qudsi Feb 01 '10 at 05:36
  • 2
    @Juliet, in my case I need a `Guid?` as key. You can see the `bool?` as the simplest available example. – ANeves Sep 07 '11 at 11:49
  • 1
    It's a good question, especially since a HashSet handles null entries just fine. – hypehuman Jun 16 '21 at 13:21

11 Answers11

39

It would tell you the same thing if you had a Dictionary<SomeType, string>, SomeType being a reference type, and you tried to pass null as the key, it is not something affecting only nullable type like bool?. You can use any type as the key, nullable or not.

It all comes down to the fact that you can't really compare nulls. I assume the logic behind not being able to put null in the key, a property that is designed to be compared with other objects is that it makes it incoherent to compare null references.

If you want a reason from the specs, it boils down to a "A key cannot be a null reference " on MSDN.

If you want an example of a possible workaround, you can try something similar to Need an IDictionary implementation that will allow a null key

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Dynami Le Savard
  • 4,986
  • 2
  • 26
  • 22
  • 5
    Wrong. You can call GetHashcode on a Nullable set to null. The result is 0. – Jonathan Allen Feb 01 '10 at 04:51
  • This doesn't really answer the question. He wants to know why Nullable is acceptable when one of it's possible values will always throw an exception. – Alastair Pitts Feb 01 '10 at 05:30
  • 1
    I never said you can't call `GetHashCode()`on a `Nullable`, I said you can't call `GetHashCode()` on `null`. – Dynami Le Savard Feb 01 '10 at 06:05
  • @Jonathan, what do you think of Dynami's comment that "you can't call `GethasCode()` on `null`"? – devuxer Feb 01 '10 at 06:15
  • @DanM Well, nevermind that. Though not being able to call `GetHashCode()` on `null` seemed like an elegant way to put it, I then thought about the fact that you can also provide your own comparer in the ctor of a dictionary, which could not be using the `Equals` and `GetHashCode` methods. – Dynami Le Savard Feb 01 '10 at 06:29
  • 2
    You can pass null to an IEQualityComparer's GetHashCode() though, so you can certain get a hash-code for a null. – Jon Hanna Aug 17 '10 at 02:05
  • 1
    @Dynami Le-Savard But you *can* compare nulls. `null == null`, `null != (bool?)true`, `null != (bool?)false`, etc. In my case I have a bidimensional Dictionary, and one of the indexers is something that can be null. I will need to work around this limitation by design, which is fine. But I would like to understand the limitation, and in this light your answer does not answer OP's question - it just re-states it. We know it cannot be null, but **why**? – ANeves Sep 07 '11 at 11:59
  • 4
    Null should be allowed as a key. It's hashcode should obviously be zero, and you can compare null to null in C# just fine; they are equal. This is not TSQL with some weird null != null concept. This implementation is terrible. For example, I want to build a tree from a flat list of item->parent relationships, so I want to call `items.GroupBy(x => x.ParentId).ToDictionary(x => x.Key, x.ToList())`, but I cannot, because the root nodes have null for their ParentId, and although it can GroupBy the value null just fine, it refuses to allow it as a key in a dictionary. Ridiculous. j/k – Triynko Jan 25 '17 at 14:31
  • 1
    Actually, there is no fundamental reason. HashSet allows null and a HashSet is simply a Dictionary where the key is the same type as the value. So, really, null keys should have been allowed but would be breaking to change it now so we're stuck with it. – Christopher King Apr 07 '17 at 19:15
  • Hashcode are meant to be evenly distributed 0 for null is not hence it stuffs up your bucketing. Not to mention as others have that dictionary is a reference structures and used the memory address .. &null == fault – user1496062 Jul 03 '17 at 03:51
  • In my opinion, this should be allowed to be circumvented by a custom IEqualityComparer. The default implementation indeed calls GetHashCode on the passed object, but a custom IEqualityComparer is free to do whatever it wants, as long as it generates hash codes consistent with the rules for GetHashCode(). The only reason for not allowing null-keys is backward compatibility, which to me sometimes feels like a bad thing. – Tom Lint Jun 22 '21 at 13:41
16

Oftentimes you have to go back to C++ methodologies and techniques to fully understand how and why the .NET Framework works in a particular manner.

In C++, you oftentimes have to pick a key that will not be used - the dictionary uses this key to point to deleted and/or empty entries. For instance, you have a dictionary of <int, int>, and after inserting an entry, you delete it. Rather than running the Garbage Cleanup right then and there, restructuring the dictionary, and leading to bad performance; the dictionary will just replace the KEY value with the key you previously selected, basically meaning "when you're traversing the dictionary memoryspace, pretend this <key,value> pair does not exist, feel free to overwrite it."

Such a key is also used in dictionaries that pre-allocate space in buckets in a particular manner - you need a key to "initialize" the buckets with instead of having a flag for each entry that indicates whether or not its contents are valid. So instead of having a triple <key, value, initialized> you would have a tuple <key, value> with the rule being that if key == empty_key then it hasn't been initialized - and therefore you may not use empty_key as a valid KEY value.

You can see this sort of behavior in the Google hashtable (dictionary for you .NET people :) in the documentation here: http://google-sparsehash.googlecode.com/svn/trunk/doc/dense_hash_map.html

Look at the set_deleted_key and set_empty_key functions to get what I'm talking about.

I'd wager .NET uses NULL as either the unique deleted_key or empty_key in order to do these sort of nifty tricks that improve performance.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • Insightful. But what about `Dictionary`? It cannot use `null` as key, and it accepts `default(int)` as a key; maybe it uses `Nullable` internally as key instead of `int`? – ANeves Sep 07 '11 at 12:03
  • Well, int is not a nullable type, so you naturally would not be able to use int(null) as a key. – Mahmoud Al-Qudsi Sep 13 '11 at 02:37
  • 3
    ... yes; let me try to rephrase. So what would then be the "key that will not be used" that would be used to mark the deleted entries? Unless the internal key type is the selected value type boxed into a `Nullable`, there is no available key as all keys are valid. No? – ANeves Sep 13 '11 at 12:37
  • Very good question - you're right. But having a key set aside as empty (and possibly another key as deleted) is an optimization. I obviously know nothing of the internal .NET code and how it's implemented, but a) it's not hard to envision that such an optimization was only used for reference types, and b) possibly completely different implementations of a hashtable are used for value and pointer types internally due to their different requirements and performance characteristics? Just a guess. – Mahmoud Al-Qudsi Feb 20 '12 at 21:30
  • Looking at the [source](http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,464) it does set the key to `default` when you remove an entry.... but I'm not sure it uses that to decide whether a spot is free or not (I see a lot of `-1` values that [I think might do that](http://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,62)). – Jeff B May 26 '15 at 14:24
  • Dictionary uses a hashcode from key.GetHashcode() as the key, not the key value itself (for numbers up to 32 bit hashcode and value are the same). And it uses 31 bits of the hashcode for comparison with 1 sign bit to indicate unset entry locations, hence the -1. – brandon Oct 04 '15 at 11:21
4

You can't use a null bool? because nullable types are meant to act like reference types. You can't use a null reference as a dictionary key, either.

The reason you can't use a null reference as a dictionary key probably comes down to a design decision at Microsoft. Allowing null keys requires checking for them, which makes the implementation slower and more complicated. For example, the implementation would have to avoid using .Equals or .GetHashCode on a null reference.

I agree that allowing null keys would be preferable, but it's too late to change the behavior now. If you need a workaround you can write your own dictionary with allowed null keys, or you could write a wrapper struct which implicitly converts to/from T and make that the key-type for your dictionary (ie. the struct would wrap the null and handle comparing and hashing, so the dictionary never 'sees' the null).

Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
4

There is no fundamental reason. HashSet allows null and a HashSet is simply a Dictionary where the key is the same type as the value. So, really, null keys should have been allowed but would be breaking to change it now so we're stuck with it.

Christopher King
  • 1,034
  • 1
  • 8
  • 21
2

Dictionairies (basic description)
A dictionary is the generic (typed) implementation of the Hashtable class, introduced in .NET framework 2.0.

A hashtable stores a value based on a key (more specificly a hash of the key).
Every object in .NET has the method GetHashCode.
When you insert an key value pair into an hashtable, GetHashCode is called on the key.
Think of it: you can't call the GetHashCode method on null.

So what about Nullable types?
The Nullable class is simply a wrapper, to allow null values to be assigned to value types. Basically the wrapper consists of an HasValue boolean that tells if it is null or not, and a Value to contain the value of the value type.

Put it together and what do you get
.NET doesn't really care what you use as a key in a hashtable/dictionary.
But when you add a key value combination, it has to be able to generate a hash of the key.
It doesn't matter if your value is wrapped inside a Nullable, null.GetHashCode is impossible.

The Indexer property and Add methods of the Dictionary will check for null, and throw an exception when it finds null.

Yvo
  • 18,681
  • 11
  • 71
  • 90
  • But the hashcode is not, by definition, assuredly unique. `public override int GetHashCode() { return 0; }` is a valid implementation, even if quite useless. So it would not matter if the Dictionary just internally used an arbitrary hashcode for null values. I'm not convinced this is a good explanation. – ANeves Sep 07 '11 at 12:10
  • However null does not equal null. Using an arbitrary code for null would result in some unpredictable code. – Yvo Sep 08 '11 at 11:02
  • 2
    I disagree, I believe null equals null. If `a==a` *must* return true, then `null==null` must also return true; no? Also, the [example implementation](http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx#sectionToggle1) provided in MSDN returns true if both are null. Am I thinking wrongly about this? – ANeves Sep 08 '11 at 15:42
1

Not using null is part of the contract according to the MSDN page: http://msdn.microsoft.com/en-us/library/k7z0zy8k.aspx

I guess the reason is that having null as valid value will just complicate the code for no reason.

0

Key value must to be unique, so null cannot be a valid key because null indicate no key.

That why .Net framework doesn't allow null value and throw an exception.

As far as why Nullable allowed, and not catches at compile time, I think the reason is because that where clause that allow everyt T except Nullable one is not possible (at least I don't know how to achieve that).

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
0

Ah, the problems of generic code. Consider this block via Reflector:

private void Insert(TKey key, TValue value, bool add)
{
    int freeList;
    if (key == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    }

There is no way to rewrite this code to say "Nullable nulls are allowed, but don't allow reference types to be null".

Ok, so how ab out not allowing TKey to be a "bool?". Well again, there is nothing in the C# language that would allow you to say that.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • 1
    typeof(T).IsValueType will return true for bool? (or any Nullable type) so it is possible to determine if it's a value nullable vs a reference null. Which brings it back to it being an implementation decision – jeffora Feb 01 '10 at 05:15
0

Dictionaries can't accept null reference types for a variety of reasons, not least because they don't have a GetHashCode method.

A null value for nullable value type is meant to represent a null value – the semantics are meant to be as synonymous with a reference null as possible. It would be slightly odd if you could use null nullable value where you couldn't use null references just because of an implementation detail of nullable value types.

Either that or Dictionary has:

if (key == null)

and they never really thought about it.

ICR
  • 13,896
  • 4
  • 50
  • 78
-1

Dictionary keys can't be null in .NET, regardless of the type of the key (nullable or otherwise).

From MSDN: As long as an object is used as a key in the Dictionary<(Of <(TKey, TValue>)>), it must not change in any way that affects its hash value. Every key in a Dictionary<(Of <(TKey, TValue>)>) must be unique according to the dictionary's equality comparer. A key cannot be null reference (Nothing in Visual Basic), but a value can be, if the value type TValue is a reference type. (http://msdn.microsoft.com/en-us/library/xfhwa508.aspx)

jeffora
  • 4,009
  • 2
  • 25
  • 38
  • 1
    Yes, we already know that. The question is why they choose to implement it that way. – Jonathan Allen Feb 01 '10 at 04:53
  • The question was specific to nullable types and I was highlighting that they behave the same as reference types – jeffora Feb 01 '10 at 04:58
  • They don't behave the same. For example, you can get the hascode of a structure null but not a reference null. – Jonathan Allen Feb 01 '10 at 05:06
  • 1
    There's no prohibition against using a type with a stupid implementation of GetHashCode. For instance, you could create your own type that, for GetHashCode, always returns 1, or returns a random value. You could still use this type as a Dictionary key. You *shouldn't* do that, of course, but that's different from saying you *can't*. – Ryan Lundy Feb 01 '10 at 05:41
-1

I've just been reading up on this; and as Eric replied, I now believe this is incorrect, not all strings are automatically Interned, and I needed to override the equality operation.


This bit me when I converted a dictionary from using a string as a key to an array of bytes.

I was in the vanilla C mindset of a string simply being an array of characters, so it took me a while to figure out why a string built by concatenation worked as a key for lookups while a byte array built in a loop did not.

It's because internally .net assigns all strings that contain the same value to the same reference. (it's called 'Interning')

so, after running:

{
string str1 = "AB";
string str2 = "A";
str1 += "C";
str2 += "BC";
}

str1 and str2 actually point to the exact same place in memory! which makes them the same onject; which allows a dictionary to find an item added with str1 as a key by using str2.

while if you:

{
char[3] char1;
char[3] char2;
char1[0] = 'A';
char1[1] = 'B';
char1[2] = 'C';
char2[0] = 'A';
char2[1] = 'B';
char2[2] = 'C';
}

char1 and char2 are distinct references; if you use char1 to add an item to a dictionary, you cannot use char2 to look it up.

  • 2
    No, this is wrong. Strings are not interned if they are the result of a computation. Your issue was that byte[] does not override GetHashCode and Equals, so you get reference comparisons, but string does, to provide value comparison. – erikkallen Mar 06 '10 at 17:50
  • surely that's what you'd expect! Just because two objects have the same values doesn't make them the same object. In c# strings are immutable so all operations actually give you a new string – JonnyRaa Nov 29 '12 at 13:38