Dictionary<T1,T2>在C#中合并2个或更多字典()的最佳方法是什么?(像LINQ这样的3.0功能很好).
我正在考虑一种方法签名:
public static Dictionary<TKey,TValue>
Merge<TKey,TValue>(Dictionary<TKey,TValue>[] dictionaries);
Run Code Online (Sandbox Code Playgroud)
要么
public static Dictionary<TKey,TValue>
Merge<TKey,TValue>(IEnumerable<Dictionary<TKey,TValue>> dictionaries);
Run Code Online (Sandbox Code Playgroud)
编辑:从JaredPar和Jon Skeet得到一个很酷的解决方案,但我正在考虑处理重复键的东西.在发生碰撞的情况下,只要它是一致的,将哪个值保存到dict并不重要.
如果我不想多次调用".Add()",那么向Dictionary添加多个值的最佳方法是什么? 编辑:我想在启动后填写它!字典中已有一些值!
而不是
myDictionary.Add("a", "b");
myDictionary.Add("f", "v");
myDictionary.Add("s", "d");
myDictionary.Add("r", "m");
...
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
myDictionary.Add(["a","b"], ["f","v"],["s","d"]);
Run Code Online (Sandbox Code Playgroud)
有这样的方法吗?
如果我实例化一个新的,Dictionary我可以传递许多值:
Dictionary<string, string> data = new Dictionary<string, string>(){
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" },
{ "Key4", "Value4" },
{ "Key5", "Value5" },
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我已经有了一个Dictionary,例如在参数中传递它时,则需要调用Add每个键值对:
data.Add("Key1", "Value1");
data.Add("Key2", "Value2");
data.Add("Key3", "Value3");
data.Add("Key4", "Value4");
data.Add("Key5", "Value5");
Run Code Online (Sandbox Code Playgroud)
我想知道是否存在一种“速记”方法来一次将大量值添加到现有字典中-最好是本机地?如果是这样的话,可以接受权威的“否”。
不像我想要的那样干净,但这是我所知道的两个选择。
这个允许一次传递多个值,但是需要创建一个新值Dictionary而不是更新现有值:
Dictionary<string, string> newData = new Dictionary<string, string>(data)
{
{ "Key6", "Value6"},
{ "Key7", "Value7"},
{ "Key8", "Value8"},
};
Run Code Online (Sandbox Code Playgroud)
也可以创建一个扩展方法,但这仍然需要Add每一行:
public static void AddMany<Tkey, TValue>(this Dictionary<Tkey, TValue> dict, …Run Code Online (Sandbox Code Playgroud) 我找不到关于这个主题的相关主题,所以我决定提出一个新问题.我是C#Dictionary课程的第一步,我有几个基本问题.首先,这个有什么区别:
Dictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "hello";
Run Code Online (Sandbox Code Playgroud)
这个(如果有的话):
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "hello");
Run Code Online (Sandbox Code Playgroud)
我的第二个问题是:是否有任何方法可以在同一行添加多个条目(如果我可以调用一对键和值).有点像int a, b, c;,而不是int a; int b; int c;.或者只能在多行上添加它们:
dict.Add(1, "hello");
dict.Add(2, "hey");
Run Code Online (Sandbox Code Playgroud)
我关于字典输出的最后一个问题是,如果有一种"更好"的方式来打印字典,而不是这样做:
foreach (KeyValuePair<int, string> pair in dict)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
Run Code Online (Sandbox Code Playgroud)
就更快打字和更快执行而言,我的意思是"更好".(我知道它也适用var,而不是KeyValuePair<>.
有没有比使用下面示例中的两个选项之一复制/添加source字典的所有内容更简单的方法destination Dictionary<T1, T2>?
Dictionary<string, int> source = new Dictionary<string, int>(),
destination = new Dictionary<string, int>();
source.Add("Developers", 1);
source.Add("just", 2);
source.Add("wanna have", 3);
source.Add("FUN!", 4);
// Option 1 (feels like a hack):
//
source.All(delegate(KeyValuePair<string, int> p)
{
destination.Add(p.Key, p.Value);
return true;
});
// Option 2:
//
foreach (string k in source.Keys)
{
destination.Add(k, source[k]);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是类似的东西.ForEach().