Why can't you use null as a key for a Dictionary?

后端 未结 11 1905
长情又很酷
长情又很酷 2020-12-24 00:33

Apparently, you cannot use a null for a key, even if your key is a nullable type.

This code:

var nullableBoolLabels = new System.Collect         


        
11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 00:52

    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.

提交回复
热议问题