Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using many dictionaries within dictionaries in my code

Tags:

c#

dictionary

I'm using a lot of dictionary collections that contain other dictionary collections like:

Dictionary<Guid, List<string>>()

Dictionary<Guid, Dictionary<Guid, List<string>>>()

I'm looping through these maps, and passing them around in parameters in my code.

This seems like a bad idea since you can't really extend the nature of these collections now.

Should I wrap these around in a class?

like image 293
codecompleting Avatar asked Nov 24 '11 23:11

codecompleting


People also ask

Can you have dictionaries within dictionaries?

A dictionary can contain dictionaries, this is called nested dictionaries.

Can you have multiple dictionaries in Python?

Creating a Nested Dictionary In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.

Can dictionaries be values in other dictionaries?

But the key values of the dictionary contain different types of values that don't need to maintain any order. When one or more dictionaries are declared inside another dictionary then it is called a nested dictionary or dictionaries of the dictionary.


2 Answers

Do you run into such limitations? Is your program hard to alter/debug? If so, refactor. Otherwise, profit: you're a pragmatic programmer,.

That said, I can see room for immediate improvement:

IDictionary<Guid, List<string>> x;

IDictionary<Guid, IDictionary<Guid, List<string>> y = new Dictionary<Guid, IDictionary<Guid, List<string>>();
like image 131
sehe Avatar answered Oct 30 '22 14:10

sehe


I'd say yes, create a class specifically for it. Then you can add/not-implement methods as per your usage, rather than working around any limitations you find with Dictionary with your usage.

It sounds like you'd want a tree-like structure.

like image 5
George Duckett Avatar answered Oct 30 '22 12:10

George Duckett