Federico
Federico

Reputation: 799

Create a dictionary/look up table in C#

I need to store opening times of shops in a file, every shop has 7 differents opening times, one for every day of the week and I have a like 2000 shops.

Instead of wasting space I figured out I could write only a indexkey and store only the opening times that are not equal to each other in a separate file. But to do so I need an hashtable/lookup table to check if they opening time I'm inserting it's or it's not already in the file.

This is the code so far:

orarioString = DateToSec(Opening) + DateToSec(Closing);
int value;
if(orarioDict.TryGetValue(orarioString, out value))
{
    sw.Write("{0,4}", value);
}
else
{
    orarioDict.Add(orarioString, index++);
    sw.Write("{0,4}", index);
}

orarioDict is a Dictionary<String,int> but I don't know it's the correct data structure because I need to insert and keep track of my "index" manually. is there a better way to do this?

Upvotes: 1

Views: 2438

Answers (1)

JaredPar
JaredPar

Reputation: 754545

I think the best solution here is Dictionary<string, List<int>>. This is the logical mapping of store key to a list of times per store. I think the worry about space here is unwarranted especially since List<int> can be sized to exactly the number of desired entries.

Upvotes: 2

Related Questions