0

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.

  • Do you need that sort of mapping in a class? You can have a collection of states and a collection of cities. A state can have a collection of cities and/or a city can have a state. – Scott Hannen Mar 12 '18 at 16:03

2 Answers2

4

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

Dictionary<State, List<City>> yourObject

I hope this will help you :)

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56
2

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.

Tom Dee
  • 2,516
  • 4
  • 17
  • 25