Maps in C#

Maps are C#’s built-in associative data type, typically implemented as dictionaries. Here’s how you can work with maps (dictionaries) in C#:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Create an empty dictionary
        var m = new Dictionary<string, int>();

        // Set key/value pairs using the typical syntax
        m["k1"] = 7;
        m["k2"] = 13;

        // Print the dictionary
        Console.WriteLine("map: {" + string.Join(", ", m) + "}");

        // Get a value for a key
        int v1 = m["k1"];
        Console.WriteLine("v1: " + v1);

        // Accessing a key that doesn't exist returns the default value for the type
        if (m.TryGetValue("k3", out int v3))
        {
            Console.WriteLine("v3: " + v3);
        }
        else
        {
            Console.WriteLine("v3: 0"); // Since default(int) is 0
        }

        // The Count property returns the number of key/value pairs
        Console.WriteLine("len: " + m.Count);

        // Remove a key/value pair using the Remove method
        m.Remove("k2");
        Console.WriteLine("map: {" + string.Join(", ", m) + "}");

        // Clear all key/value pairs
        m.Clear();
        Console.WriteLine("map: {" + string.Join(", ", m) + "}");

        // Checking if a key exists and its associated value
        m["k2"] = 13;
        if (m.TryGetValue("k2", out _))
        {
            Console.WriteLine("prs: true");
        }
        else
        {
            Console.WriteLine("prs: false");
        }

        // Declare and initialize a new dictionary
        var n = new Dictionary<string, int>
        {
            { "foo", 1 },
            { "bar", 2 }
        };
        Console.WriteLine("map: {" + string.Join(", ", n) + "}");

        // Using LINQ to check if two dictionaries are equal
        var n2 = new Dictionary<string, int>
        {
            { "foo", 1 },
            { "bar", 2 }
        };
        bool areEqual = DictionariesAreEqual(n, n2);
        if (areEqual)
        {
            Console.WriteLine("n == n2");
        }
    }

    // Helper method to check if two dictionaries are equal
    static bool DictionariesAreEqual(Dictionary<string, int> dict1, Dictionary<string, int> dict2)
    {
        if (dict1.Count != dict2.Count)
            return false;

        foreach (var kvp in dict1)
        {
            if (!dict2.TryGetValue(kvp.Key, out int value))
                return false;
            if (value != kvp.Value)
                return false;
        }
        return true;
    }
}

To create an empty dictionary, use the Dictionary<key-type, value-type>. Set key/value pairs using the typical dict[key] = value syntax. Retrieve a value for a key with dict[key], and use the Count property to get the number of key/value pairs. Use Remove(key) to delete a key, and Clear() to remove all key/value pairs. The TryGetValue method is useful to check if a key exists without throwing an exception. You can also declare and initialize a new dictionary in the same line using collection initializer syntax. Finally, you can use a helper function or LINQ to compare two dictionaries for equality.