An n
-by-2 array allows this initializer syntax:
string[,] z = { { "a", "b" }, { "c", "d" }, { "z", "w" }, };
In this case dimensions are 3-by-2.
If you prefer an array of arrays, string[][]
, the syntax is just a bit more clumpsy. Some rows could accidentally (or intentionally) have a different length than the others ("jagged" array). An array of arrays might be easier to order using Array.Sort
on the "outer" array, with some IComparer<string[]>
or Comparison<string[]>
.
Otherwise, use SortedDictionary<string, string>
or SortedList<string, string>
as already suggested in a comment by spender
:
var z = new SortedDictionary<string, string> { { "a", "b" }, { "c", "d" }, { "z", "w" }, };
Any type, including user-defined types, that has an (accessible and non-static) Add
method which takes in two strings, will allow this sort of collection initializer. For example:
var z = new YourType { { "a", "b" }, { "c", "d" }, { "z", "w" }, };
will be roughly equivalent to:
YourType z;
{
var temp = new YourType();
temp.Add("a", "b");
temp.Add("c", "d");
temp.Add("z", "w");
z = temp;
}
// use variable z here