C# 在C中初始化没有值的字典#

C# 在C中初始化没有值的字典#,c#,dictionary,C#,Dictionary,我想创建只带键的字典。例如: Dictionary <string, double> SmplDict= new Dictionary<string, double>(); SmplDict.Add("KeyOne"); SmplDict.Add("KeyTwo"); 但它不起作用。您不能将null值放在需要double值的位置,因为它不是引用类型,而是值类型: Dictionary <string, double> 另一方面,如果要保持putnulls

我想创建只带键的字典。例如:

Dictionary <string, double> SmplDict= new Dictionary<string, double>();
SmplDict.Add("KeyOne");
SmplDict.Add("KeyTwo");

但它不起作用。

您不能将
null
值放在需要
double
值的位置,因为它不是引用类型,而是值类型:

Dictionary <string, double>

另一方面,如果要保持put
null
s,请将
double
值标记为可空:

Dictionary <string, double?>
字典

如果希望值为null,则选择一种可为
值为null的类型。例如
双精度?
对象
等。

如果要将
值设为null,则必须将其设为null:

Dictionary <string, double?> SmplDict= new Dictionary<string, double?>();

虽然double不能为null,但存在一个非数字常量:

Dictionary <string, double?> SmplDict= new Dictionary<string, Double.NaN>();
Dictionary SmplDict=newdictionary();
你可以查一下

Dictionary <string, double?> SmplDict= new Dictionary<string, double?>();
SmpDict.Add("KeyOne", null)
Dictionary <string, double?> SmplDict= new Dictionary<string, Double.NaN>();