为了我的代码,我需要一个包含两个组件的元组,这两个组件都是KeyValuePairs。然而,在我的生命中,我甚至不知道如何声明这个东西。我让它在普通字符串上工作。
Tuple<string, string> t = new Tuple<string, string>("abc", "123");
但是我需要KeyValue对而不是字符串,我尝试过这样的方法,但是它拒绝编译,因为构造函数不能接受2个参数。
Tuple<KeyValuePair<string, string>, KeyValuePair<string,string>> a =
new Tuple<KeyValuePair<string, string> ("a", "1"),
KeyValuePair<string, string> ("b", "2");
任何指导都将不胜感激。如果对您有帮助,请随时使用:https://dotnetfiddle.net/y2rTlM
发布于 2019-03-01 07:32:59
使用:
Tuple<KeyValuePair<string, string>, KeyValuePair<string, string>> a =
new Tuple<KeyValuePair<string, string>, KeyValuePair<string, string>>(
new KeyValuePair<string, string>("a", "1"),
new KeyValuePair<string, string>("b", "2")
);
发布于 2019-03-01 08:26:12
或者,简而言之:
using KVPS = System.Collections.Generic.KeyValuePair<string, string>;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Tuple<KVPS, KVPS> a =
Tuple.Create(
new KVPS("a", "1"),
new KVPS("b", "2")
);
Console.WriteLine($"{a.Item1.Key} {a.Item1.Value} : {a.Item2.Key} {a.Item2.Value}");
}
}
}
如果你有很多元组和类似的元组,这可能会很有用。
https://stackoverflow.com/questions/54935871
复制相似问题