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?
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?
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
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.
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>>
If you need to look up the date from the two-digit string, you could use Dictionary<string, int>
from System.Collections.Generic
.
Dictionary<string,Int32>
seems to be the best choice given the limit details
That depends, some options:
List
or collection of each of those linesDictionary<string, int>
, assuming the "AL", "AK" etc is unique and you want to look up the right-hand side based on the left-hand sideDataSet
--> 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. ;)