3

I have to store this data:

 "AL" => 1997
 "AK" => 1977
 ...
 "WY" => 1997

What's the best way to store this in .NET? Shall I use just arrays, or arrayList, or another collection?

cdub
  • 24,555
  • 57
  • 174
  • 303

8 Answers8

12

Try System.Collections.Generic.Dictionary<TKey, TValue>.

You would use it like this:

var values = new Dictionary<string, int>
{
    { "AL", 1997 },
    { "AK", 1977 },
    ...
    { "WY", 1997 },
};

Console.WriteLine(values["AK"]);  // Writes 1977
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
4

This is a typical key -> value storage problem, so your best option would be

SortedList<string, int> 

or

Dictionary<string, int>     

As the question here is which is the best one suitable, here you go:

Choosing between the two depends on your usage. If you'll instantiate the list all at once then SortedList is your answer as it uses less memory and is a bit faster than Dictionary (SortedDictionary). If you intend to insert or delete items, then Dictionary should be your pick as it is a bit faster there.

Source, originally from MSDN

Community
  • 1
  • 1
edvaldig
  • 2,301
  • 16
  • 17
  • Could you give us a little information about the difference between those two, and which one you would choose in this situation? – DOK Oct 10 '11 at 19:34
3

First of all, by rule of thumb you should not use non-generic collection types for known values, unless you're using a version of .NET prior to 2.0

For unique keys

Dictionary<string,int32> or Dictionary<string,string>

For non-unique keys

List<KeyValuePair<string,int32>> or List<KeyValuePair<string,string>>
cwharris
  • 17,835
  • 4
  • 44
  • 64
  • 1
    shouldn't that be ".NET 1.x"? – David Oct 10 '11 at 19:41
  • 2
    "Never" is pretty strong language. Can you give a reason for your statement? – DOK Oct 10 '11 at 19:50
  • 1
    @DOK, Never is a strong word. Lets say rule of thumb in stead. However, the only benefit to using non-generic collection types over generic collection types is the size of your library in memory. When a new generic type is initialized with a new combination of generic parameters, a new type is generated and stored in the in-memory assembly. Therefore each type used requires (a little bit) more memory. By using non-generic collections, you only ever have one collection type (of that type) in memory at a time, therefore reducing memory usage. That's the only exception I can think of. – cwharris Oct 11 '11 at 06:08
2

Use a Dictionary<string, int> if your keys are unique.

rohit89
  • 5,745
  • 2
  • 25
  • 42
2

If you need to look up the date from the two-digit string, you could use Dictionary<string, int> from System.Collections.Generic.

James
  • 7,343
  • 9
  • 46
  • 82
1

Dictionary<string,Int32> seems to be the best choice given the limit details

yas4891
  • 4,774
  • 3
  • 34
  • 55
1

That depends, some options:

  • As a string (array of chars) containing that whole paragraph
  • As an array, List or collection of each of those lines
  • As a Dictionary<string, int>, assuming the "AL", "AK" etc is unique and you want to look up the right-hand side based on the left-hand side
  • In a DataSet

--> It depends ENTIRELY on what you're doing with the data, what the constraints are, what the requirements are. Give us some more information and you'll get a very detailed answer. <--

E.g.: first option if you're trying to store that data as some text for display on a web page. The second option as the initial part of parsing some text entered by a user or imported from a file. The third option if you are building it in code, or intend to query or otherwise perform logic within the code, or an object model. The final one if you're going to/from an ADO.NET data source. There are more.. You could use a BinaryStream object containing a JBIG2-encoded, dithered image of the text. ;)

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
1

If you want it to be sorted, you could use a SortedDictionary

Guy Starbuck
  • 21,603
  • 7
  • 53
  • 64