Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向字典添加值,使其值为列表_C# - Fatal编程技术网

C# 向字典添加值,使其值为列表

C# 向字典添加值,使其值为列表,c#,C#,我有一本这样的字典: Dictionary<string, List<string>> providerLevelChanges = new Dictionary<string, List<string>>(); 您需要确保列表存在,如果不存在,则添加它 List<string> list; if(!providerLevelChanges.TryGetValue(someKey, out list)) { list = ne

我有一本这样的字典:

Dictionary<string, List<string>> providerLevelChanges = new Dictionary<string, List<string>>();

您需要确保列表存在,如果不存在,则添加它

List<string> list;
if(!providerLevelChanges.TryGetValue(someKey, out list))
{
    list = new List<string>();
    providerLevelChanges.Add(someKey, list);
}

list.Add(someNewValue);
列表;
如果(!providerLevelChanges.TryGetValue(someKey,out list))
{
列表=新列表();
providerLevelChanges.Add(someKey,list);
}
添加(someNewValue);

您首先需要初始化列表,您可以使用检查密钥是否已存在,否则添加它:

List<string> list;
if (!providerLevelChanges.TryGetValue("someKey", out list))
{
    list = new List<string>();
    providerLevelChanges.Add("someKey", list);
}
list.Add("someNewValue");
列表;
如果(!providerLevelChanges.TryGetValue(“someKey”,输出列表))
{
列表=新列表();
providerLevelChanges.Add(“someKey”,列表);
}
添加(“someNewValue”);
如果要在一行中初始化词典,可以使用集合初始值设定项:

Dictionary<string, List<string>> providerLevelChanges = new Dictionary<string, List<string>>()
{
    { "someKey", new List<string>{"someNewValue"} }
};
Dictionary providerLevelChanges=new Dictionary()
{
{“someKey”,新列表{“someNewValue”}
};

您需要首先创建您的价值列表。 试试像这样的东西

List<string> theList = new List<string>()
if (!providerLevelChanges.TryGetValue("somekey", out theList))
{
   //The key is not present in the dictionary 
   providerLevelChanges["somekey"] = theList;
}
theList.Add("someNewValue");
List theList=新列表()
如果(!providerLevelChanges.TryGetValue(“somekey”,在列表外))
{
//字典里没有钥匙
providerLevelChanges[“somekey”]=列表;
}
添加(“someNewValue”);

如果没有完整的代码和错误消息,很难判断问题出在哪里,但下面是一个完整的示例:

//instantiate the Dictionary
Dictionary<string, List<string>> providerLevelChanges = new Dictionary<string, List<string>>();

//Add the first item to the Dictionary - notice the empty List<string>
providerLevelChanges.Add("someKey", new List<string>());

//Add a value to the new List<string>
providerLevelChanges["someKey"].Add("someNewValue");
//实例化字典
Dictionary providerLevelChanges=新字典();
//将第一项添加到字典-注意空列表
providerLevelChanges.Add(“someKey”,newlist());
//向新列表中添加一个值
providerLevelChanges[“someKey”]。添加(“someNewValue”);

重要的一点是,在尝试访问字典之前,我正在向字典中添加一个完整的元素。

“但它是错误的”;它有什么问题?是的,错误或异常是什么?它应该工作。有什么问题吗?我得到空值exception@Bohn听起来您可能在某个时候做了
providerlevelchanges.Add(“someKey”,null)
//instantiate the Dictionary
Dictionary<string, List<string>> providerLevelChanges = new Dictionary<string, List<string>>();

//Add the first item to the Dictionary - notice the empty List<string>
providerLevelChanges.Add("someKey", new List<string>());

//Add a value to the new List<string>
providerLevelChanges["someKey"].Add("someNewValue");