cdub
cdub

Reputation: 25751

Best collection for data in C# .NET

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?

Upvotes: 3

Views: 3108

Answers (8)

cwharris
cwharris

Reputation: 18125

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>>

Upvotes: 3

edvaldig
edvaldig

Reputation: 2299

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

Upvotes: 4

Guy Starbuck
Guy Starbuck

Reputation: 21873

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

Upvotes: 1

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

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. ;)

Upvotes: 1

James
James

Reputation: 7543

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

Upvotes: 2

rohit89
rohit89

Reputation: 5773

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

Upvotes: 2

yas4891
yas4891

Reputation: 4862

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

Upvotes: 1

Bryan Watts
Bryan Watts

Reputation: 45465

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

Upvotes: 12

Related Questions