将 Dictionary<int, List<string>> 分配给空的新声明字典

Dvl*_*lpr 3 .net c# dictionary list

我有一本字典,如下所示:

var objListColor = new List<String>() { "Red", "Blue", "Green", "Yellow" };
var objListDirection = new List<String>() { "East", "West", "North", "South" };

var dictFrom = new Dictionary<int, List<String>>();
dictFrom.Add(1, objListColor);
dictFrom.Add(2, objListDirection);
Run Code Online (Sandbox Code Playgroud)

我有另一个字典声明,如下所示:

var dictTo = new Dictionary<int, List<String>>();
Run Code Online (Sandbox Code Playgroud)

我想填充dictTodictFrom键和值。如何使用 LINQ 或 C# 进行填充?我想用 foreach 或 for 循环分配值和键。

Bri*_*Kay 5

字典有一个复制构造函数:

var dictTo = new Dictionary<int, List<String>>(dictFrom);
Run Code Online (Sandbox Code Playgroud)

这会将 dictFrom 的内容复制到 dictTo 中。修改 dictTo 不会影响 dictFrom (反之亦然),因为 dictTo 是实际的浅拷贝。

列表和其他一些集合具有类似的功能。

MSDN:https://msdn.microsoft.com/en-us/library/et0ke8sz (v=vs.110).aspx