Reputation: 191
For the sake of my code I need a tuple which has 2 components both of which are KeyValuePairs. However, for the life of me I can't even figure out how to declare this thing. I had it working with normal strings
Tuple<string, string> t = new Tuple<string, string>("abc", "123");
But I need to have KeyValue Pairs instead of strings, I've tried something like this but it refuses to compile saying that constructor can't take 2 arguments.
Tuple<KeyValuePair<string, string>, KeyValuePair<string,string>> a =
new Tuple<KeyValuePair<string, string> ("a", "1"),
KeyValuePair<string, string> ("b", "2");
Any guidance would be greatly appreciated. Please feel free to use this if it helps you: https://dotnetfiddle.net/y2rTlM
Upvotes: 1
Views: 580
Reputation:
Or, a bit shorter:
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}");
}
}
}
This might be useful if you have a lots of tuples of tuples and similar.
Upvotes: 0
Reputation: 56162
Use:
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")
);
Upvotes: 2