In this article, you will learn how to build a three-dimensional Dictionary structure in C#.
Suppose we need to build a collection of Postal Codes in which each postal code contains a collection of Cities. Each City is itself a collection of Street Addresses, and each Street contains all the residents and their personal records. We will make this whole collection with a Dictionary collection, one of the best data structures for searching efficiency. So our "Three-dimensional dictionary collection" will look like this:
Postal Code -> City –> Street Addresses -> Resident's Records -> SSN, Name, Age, Gender and Occupation

In C#, we can implement the preceding collection in the following way:
class ThreeDimensionalDictionaryDemo
{
public Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>> PeopleCollection = new Dictionary<string, Dictionary<string, Dictionary<string, List<ResidentRecord>>>>();
public void AssemblePersonRecordByPostalCode()
{
try
{
// A data record file "RecordsFile.txt" is read.
StreamReader oStreamReader = new StreamReader("RecordsFile.txt");
string line;
string postalCode;
string city;
string streetAddress;
ResidentRecord oRecord;
while ((line = oStreamReader.ReadLine()) != null)
{
// In records file, we keep one person's record in one line and data fields are seperated by TAB
// For example:
// 933467543 John Smith 24 Male Business 21093 Timonium 2311 York Rd Suit # 44
string[] parsedItem = line.Split('\t');
// Retrieving data fields
oRecord.SSN = parsedItem[0];
oRecord.Name = parsedItem[1];
oRecord.Age = Convert.ToInt16(parsedItem[2]);
oRecord.Gender = parsedItem[3];
oRecord.Occupation = parsedItem[4];
postalCode = parsedItem[5];
city = parsedItem[6];
streetAddress = parsedItem[7];
// If we get an already existing Postal Code
if (PeopleCollection.ContainsKey(postalCode))
{
// If we get an already existing City
if (PeopleCollection[postalCode].ContainsKey(city))
{
// If we get an already existing StreetAddress
if (PeopleCollection[postalCode][city].ContainsKey(streetAddress))
{
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
// If we get a new StreetAddress
else
{
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
// If we get a new city
else
{
// City is added and value part is newed up
PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
// If we get a new postal code
else
{
// Postal code is added and value part is newed up
PeopleCollection.Add(postalCode, new Dictionary<string, Dictionary<string, List<ResidentRecord>>>());
// City is added and value part is newed up
PeopleCollection[postalCode].Add(city, new Dictionary<string, List<ResidentRecord>>());
// Street Address is added and value part is newed up
PeopleCollection[postalCode][city].Add(streetAddress, new List<ResidentRecord>());
// Person's record is added into list
PeopleCollection[postalCode][city][streetAddress].Add(oRecord);
}
}
}
catch (Exception e)
{
throw e;
}
}
}
Thanks,
Hemant Srivastava
Next > Dictionary Tutorial in C#

Ozcan ArslanPosted Sep 11, 2019, 2:52 AM
Hi, sorry if not belongs here but my question is how we sort ascending if populated dictionary array if key is something like 1.1, 1.2, 1.2.1, 1.2.2, 1.3 etc.?
Hemant SrivastavaPosted May 1, 2016, 9:07 PM
Good ! It helps you Naredla Nithesh
Naredla NitheshPosted Apr 29, 2016, 5:22 PM
Hemant,Thank you for the post....It solved my problem
Hemant SrivastavaPosted Feb 12, 2016, 4:39 PM
Good you found the solution !
armel djientPosted Feb 12, 2016, 3:32 PM
i found the solution. thanks again for these explanations
armel djientPosted Feb 12, 2016, 11:52 AM
thank you, sir, now i have a problem how to display output of three dimensional dictionary?
armel djientPosted Feb 12, 2016, 11:47 AM
i got it how it works, and i feel happy. i do the same thing on a winform but in my case data are retreived from Database.
armel djientPosted Feb 12, 2016, 8:41 AM
excuse me "ResidentRecord" represent what exactly?
Hemant SrivastavaPosted Sep 10, 2012, 10:02 PM
Thanks guys..
Dinesh BeniwalPosted Sep 10, 2012, 11:33 AM
good work Hemant.
Sam HobbsPosted Sep 9, 2012, 7:16 PM
Thank you, Hemant. This looks like a good sample of hierarchical collections. I think it is important for developers to understand that in the real world, there can be multiple postal codes for cities, at least in the United States. So for the USA, relating Zip codes and cities is more challenging.