Search code examples
.netdata-structurescollectionsordereddictionary

What is the best data structure in .NET for look-up by string key or numeric index?


I'm looking for the most ideal data structure (for performance and ease of use) from which values can be retrieved by string key or index. Dictionary doesn't work because you can't really retrieve by index. Any ideas?


Solution

  • You want the OrderedDictionary class. You will need to include the System.Collections.Specialized namespace:

        OrderedDictionary od = new OrderedDictionary(); 
        od.Add("abc", 1); 
        od.Add("def", 2); 
        od.Add("ghi", 3); 
        od.Add("jkl", 4); 
    
        // Can access via index or key value:      
        Console.WriteLine(od[1]);       
        Console.WriteLine(od["def"]);