【发布时间】:2017-03-25 09:33:37
【问题描述】:
对不起,如果这是基本的。为什么我不能将字典添加到另一个字典?给定以下代码:
var mapStudentData = new Dictionary<string, Dictionary<int, List<Student>>>();
var dicValue = new Dictionary<int, List<Student>>();
mapStudentData["strKey"].Add(dicValue);
我收到以下错误:
方法 'Add' 没有重载需要 1 个参数
任何关于这些的提示都会有很大帮助。提前致谢。
解决了!
我一直在使用下面的扩展方法,运行正常^^
public static void AddRange<T>(this ICollection<T> target, IEnumerable<T> source)
{
if(target==null)
throw new ArgumentNullException("target");
if(source==null)
throw new ArgumentNullException("source");
foreach(var element in source)
target.Add(element);
}
并像这样使用:
mapStudentData["strKey"].AddRange(dicValue);
【问题讨论】:
-
mapStudentData.Add("strKey", dicValue); -
如果这样做,添加相同的Key会抛出异常
-
感谢您的帮助。由于重复的问题,所以我将查看可用信息作为 CollinD 有说明的链接
-
或
mapStudentData["strKey"] = dicValue;看到混合这两种方法的尝试非常奇怪。
标签: c# dictionary