Why not use a class that inherits from Dictionary
?
public class PairDictionary : Dictionary<int, string>
{
}
private static void Main(string[] args)
{
var temp = new PairDictionary
{
{0, "bob"},
{1, "phil"},
{2, "nick"}
};
Console.ReadKey();
}
You could also create your own collection (I suspect it is the case because you have the same Value1
for two items, so T1
doesn't act as a key in your example) that do not inherit from Dictionary
.
If you want to use the syntactic sugar of collection initializer, you would have to provide a Add
method that takes 2 arguments (T1
and T2
which are int
and string
in your case).
public void Add(int value1, string value2)
{
}
See Custom Collection Initializers