I am dealing with Array of Dictionaries and this SO Post is really helpful to achieve what I want so far.
But, now I want to initialize Dictionary
for an array index based on the output of code.
I have a Dictionary<int,string>
, where I am storing a Id
as Key. I am having Array of 10 dictionaries as follows:
Dictionary<int, string>[] matrix = new Dictionary<int, string>[10];
So, based on the value of (Id%10)
, i want to store that record in a respective array. For i.e., if id= 12
, I want to store it in matrix[2]
. If id = 15
, i want to store it in matrix[5]
.
Now,question is, how to check each time whether a dictionary is initialized for a particular index or not. If yes, than add the record to the dictionary else initialize the instance and then add the record to Dictionary.
Something like following:
if {} // if dict with id%10 is initialized then
{
matrix[id%10].Add();
}
else
{
matrix[id%10] = new Dictionary<int,string>();
matrix[id%10].Add();
}
Edit: I know i can initialize all first using loop, but I want to initialize only when it's necessary.