了解了该问题的回答,那么如何合并两个(仅限)字典(.Net 4.5)?
不允许重复,以第一个为准(请参见下面的示例)。
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var d1 = new Dictionary<int, string>();
var d2 = new Dictionary<int, string>();
// combine both, keep first duplicate only
var result = d1.XXX?(d2);
}
}
说
[<1,'a'>, <2, 'b'>]
+ [<3,'c'>, <1, 'x'>]
= > [<1,'a'>, <2, 'b'>, <3,'c'>]
var dicts = new [] {d1, d2}
。使用join也可以,但在方法语法中它们要复杂得多。没有一个简单的内置函数可以做到这一点。 - D Stanley