To declare a multi-dimensional string array in C#, it's a simple matter of writing this:
string[,] array = new string[4,4];//size of each array here
Initializing an array is done in the same way as it is in C(++), using curly brackets:
string[,] array = new string[,] { {"index[0][0]", "index[0][1]"}, {"index[1][0]", "index[1][1]"} };
basic tut on arrays in C# here
But you're not creating an array, you're using strings as keys, meaning you need a Dictionary
:
Dictionary<string, string[]> dictionary = new Dictionary<string, string[]> {
{"first_array", new string[4]},
{"second_array", new string[4]}
};
In case of a Dictionary
, owing to its declaration being quite verbose, it's quite common to see code that relies on C#'s implicit typing to abreviate things a bit:
var dictionary = new Dictionary<string, string[]> {
{"first_array", new string[4]},
{"second_array", new string[4]}
};
more on Dictionary
here
Update:
Because you'd like to be able to append strings to the arrays as you go along (ie: you don't know the length of the array when you create it), you can't really use a regular string array. You'll have to use a dictionary of string lists:
var dictionary = new Dictionary<string, List<string>> {
"first_list", new List<string>() { "first string", "second string" },
"second_list", new List<string>() { "first string", "second string" }
};
So now, bringing it all together, and some examples of how you can add strings to lists, and add lists to the dictionary:
//add string to list in dictionary:
dictionary["first_list"].Add("third string");
//suppose we have an array, how to add to the dictionary?
string[] some_array = new string[2] {"string1", "string2"};
//simply initialize list using array of strings
var temp_list = new List<string>(some_array);
//add new key to dictionary
dictionary.Add("new_list", temp_list);
docs on the List<T>
class
Note:
Arrays can be resized, so it is possible to use an array instead of a list, but in this case, it's better to use a list. Lists are designed to be resized, arrays are not the best tool for the job