0

Not sure if the title is properly worded but basically I am trying to load all the file names of text files in a given directory, then loading the lines of text from each file, all into one collection.

I would hope to do something like this:

Dictionary<string, List<string>> db = new Dictionary<string, List<string>>;
db.Add("c:\text1.txt", new List<string>(new string[]{"line1","line2","line3"}));

And then to access it using db[0]

Is there any kind of collection to do this, or can you all recommend a different way of doing it?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
blizz
  • 4,102
  • 6
  • 36
  • 60

1 Answers1

1

Obviously, since dictionaries are unordered by default (items are stored in buckets, which depends on key hash code, the order of items doesn't matter), you have two options:

  • use ordered version of dictionary-like collection (see OrderedDictionary, KeyedCollection and this question);
  • use any ordered collection, like List<Tuple<string, List<string>>>, but this disallows to search by key.
Community
  • 1
  • 1
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • @blizz: `Tuple<...>` here is just a sample container to wrap a file name and its lines. You could use your own class as well, if `Tuple<...>` doesn't fit your needs. – Dennis Feb 21 '14 at 05:11