C# Dictionary recursion

Viewed 281

I'm searching for a construct in c# where Dictionaries contain Dictionaries to count n. I want to put a list of Dictionaries into and it builds some kind of index.

I want to call it like dic[key][key2][key3] where the value is object or a Dictionary or a Dictionary containing more Dictionaries.

I think elastic can provide something like that but our solution is a standalone client application.

1 Answers

Dictionaries can be nested like this:

        var dictionary = new Dictionary<string, Dictionary<string, int>>();

To initialise a nested dictionary:

        var dictionary = new Dictionary<string, Dictionary<string, int>>
        {
            { "a1", new Dictionary<string, int> { { "b1a", 1 }, { "b1b", 2 } } },
            { "a2", new Dictionary<string, int> { { "b2a", 3 }, { "b2b", 4 } } }
        };

You then index into the dictionary like this:

        int x = dictionary["a1"]["b1a"];
        Assert.AreEqual(1, x);

EDIT: to have an arbitrary depth, you need to create your own type that has built-in nesting, e.g.,

    class Node
    {
        public int Value { get; set; }

        public Dictionary<string, Node> Children { get; set; }

        // The indexer indexes into the child dictionary.
        public Node this[string key] => Children[key];
    }

Normally I would define Children as a List, but you want dictionaries.

Sample usage:

        var node = new Node
        {
            Children = new Dictionary<string, Node>
            {
                { "a1", new Node
                    {
                        Children = new Dictionary<string, Node>
                        {
                            { "b1a", new Node { Value = 1 } },
                            { "b1b", new Node { Value = 2 } }
                        }
                    }
                },
                { "a2", new Node
                    {
                        Children = new Dictionary<string, Node>
                        {
                            { "b2a", new Node { Value = 3 } },
                            { "b2b", new Node { Value = 4 } }
                        }
                    }
                }
            }
        };

        int y = node["a1"]["b1a"].Value;
        Assert.AreEqual(1, y);

This can go as deep as you like--just stick another Dictionary into the Children property of a leaf node.

Related