1

I need a fast lookup function that will return a string based on an integer key, and also perform the oppose lookup (pass in string, return int).

Should I create 2 hashtables for this?

Cœur
  • 34,719
  • 24
  • 185
  • 251
mrblah
  • 93,893
  • 138
  • 301
  • 415
  • 1
    If your integer key is compact enough (mostly inclusive of 0 thru N) then I would use string[] or List to map the integers to the string. As others said, use the Dictionary for the reverse. – csharptest.net Sep 22 '09 at 16:41

2 Answers2

5
Dictionary<int, string> lookup = new Dictionary<int, string>();
lookup.Add(1, "test1");
lookup.Add(2, "test2");
lookup.Add(3, "test3");

If you need to do fast lookups both ways, you could make 2 of them.

Larsenal
  • 47,632
  • 41
  • 144
  • 213
4

A Dictionary is what you want, and plenty of related answers to help.

Though hardly a definitive answer without more information, will the strings be unique for instance?

Community
  • 1
  • 1
dove
  • 20,075
  • 14
  • 85
  • 106