Andrew Buikema
Andrew Buikema

Reputation: 153

What is the best data structure to store cities and states in C#?

In my C# program, I plan to query my database for cities and states, and I want to store the information in some sort of list so that I can run a foreach loop. I considered a hash table at first, but I'm not sure that would work since there can be multiple identical keys (cities with the same name). Is there a data structure that can store pairs (city, state) or does the C# ArrayList allow some sort of mapping? Thank you in advance.

Upvotes: 0

Views: 516

Answers (2)

Tom Dee
Tom Dee

Reputation: 2674

You can create a tuple with two elements like this:

var city = new Tuple<string, string>("New York", "NY");

Then just create a standard list containing all the tuples, then you will be able to do a for each loop on it. To access the city name you can just do city.Item1 and to access the state city.Item2.

Upvotes: 2

Dina Bogdan
Dina Bogdan

Reputation: 4738

I think that you can use 2 data structures for this, such like:

Dictionary<State, List<City>> yourObject

I hope this will help you :)

Upvotes: 4

Related Questions