uri*_*ium 38 .net data-structures
我知道我们应该使用字典而不是哈希表.我找不到克隆字典的方法.即使将它转换为ICollection,我也是为了获得SyncRoot,我知道这也是不赞成的.
我现在忙着改变它.我是否正确地假设无法以通用方式实现任何类型的克隆,这就是字典不支持克隆的原因?
Fil*_*erg 57
使用带有Dictionary的构造函数.看这个例子
var dict = new Dictionary<string, string>();
dict.Add("SO", "StackOverflow");
var secondDict = new Dictionary<string, string>(dict);
dict = null;
Console.WriteLine(secondDict["SO"]);
Run Code Online (Sandbox Code Playgroud)
只是为了好玩..你可以使用LINQ!这是一种更通用的方法.
var secondDict = (from x in dict
select x).ToDictionary(x => x.Key, x => x.Value);
Run Code Online (Sandbox Code Playgroud)
编辑
这应该适用于参考类型,我尝试了以下内容:
internal class User
{
public int Id { get; set; }
public string Name { get; set; }
public User Parent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及上面的修改代码
var dict = new Dictionary<string, User>();
dict.Add("First", new User
{ Id = 1, Name = "Filip Ekberg", Parent = null });
dict.Add("Second", new User
{ Id = 2, Name = "Test test", Parent = dict["First"] });
var secondDict = (from x in dict
select x).ToDictionary(x => x.Key, x => x.Value);
dict.Clear();
dict = null;
Console.WriteLine(secondDict["First"].Name);
Run Code Online (Sandbox Code Playgroud)
其中输出"Filip Ekberg".
| 归档时间: |
|
| 查看次数: |
37071 次 |
| 最近记录: |