Compound Data Structures in C#

Compound Data Structures in C#

Welcome to our exploration of Compound Data Structures in C#. Having navigated through Dictionaries, Sets, and Arrays, we'll delve into nested Dictionaries and arrays. These structures enable us to handle complex and hierarchical data, which is typical in real-world scenarios. Nested data structures are commonly used to represent data models like organizational charts, product categories, and multi-dimensional datasets. This lesson will guide you through a recap of the basics, as well as the creation and modification of nested Dictionaries and arrays.

Recap: Dictionaries, Arrays, and Understanding Nested Structures

As a quick recap, Arrays are mutable, ordered collections, while Dictionaries are collections of key-value pairs with unique keys. These structures can be nested. Here's a simple example of a school directory:

// Dictionary with grades as keys and arrays of students as values
Dictionary<string, string[]> schoolDirectory = new Dictionary<string, string[]>
{
    { "Grade1", new string[] { "Amy", "Bobby", "Charlie" } },
    { "Grade2", new string[] { "David", "Eve", "Frank" } },
    { "Grade3", new string[] { "George", "Hannah", "Ivy" } }
};

// Logs the Grade1 array in the Dictionary
Console.WriteLine(string.Join(", ", schoolDirectory["Grade1"])); // Output: Amy, Bobby, Charlie

In this example, we have a Dictionary where each key represents a grade, and the corresponding value is an array of student names. This is a simple demonstration of a nested data structure.

Creating Nested Dictionaries and Arrays

Just like their non-nested versions, creating nested structures is straightforward.

Nested Dictionary:

// Dictionary within a Dictionary
Dictionary<string, Dictionary<string, string>> nestedDictionary = new Dictionary<string, Dictionary<string, string>>
{
    { "fruit", new Dictionary<string, string> 
        {
            { "apple", "red" },
            { "banana", "yellow" }
        }
    },
    { "vegetable", new Dictionary<string, string> 
        {
            { "carrot", "orange" },
            { "spinach", "green" }
        }
    }
};

// Logs the nested dictionary
foreach (var category in nestedDictionary)
{
    Console.WriteLine($"{category.Key}:");
    foreach (var item in category.Value)
    {
        Console.WriteLine($"  {item.Key}: {item.Value}");
    }
}

Here, we have a Dictionary that contains other Dictionaries as values. Each top-level key, such as "fruit" or "vegetable", points to another Dictionary that holds key-value pairs related to that top-level category.

Nested Array:

// Arrays within an array
int[][] nestedArray = new int[][]
{
    new int[] { 1, 2, 3 },
    new int[] { 4, 5, 6 },
    new int[] { 7, 8, 9 }
};

// Logs the nested array
foreach (int[] innerArray in nestedArray)
{
    Console.WriteLine(string.Join(", ", innerArray));
}

In this case, we create a nested array where each element of the outer array is itself an array. This structure is useful for scenarios like multi-dimensional datasets.

Nested Dictionaries and Arrays:

// Arrays within a Dictionary
Dictionary<string, int[]> arrayDictionary = new Dictionary<string, int[]>
{
    { "numbers", new int[] { 1, 2, 3 } },
    { "letters", new int[] { 10, 11, 12 } }
};

// Logs the Dictionary of arrays
foreach (var pair in arrayDictionary)
{
    Console.WriteLine($"{pair.Key}: {string.Join(", ", pair.Value)}");
}

This example shows a Dictionary where each value is an array. This pattern is practical for mapping categories to lists of values efficiently.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal