Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Dictionary<string, Dictionary<T, T[]>[]>

Tags:

c#

.net

In C#, what is the syntax for instantiating and initializing a dictionary containing as values an array of dictionaries, those dictionaries themselves containing arrays as values?

For example, (I believe),

Dictionary<string, Dictionary<string, string[]>[]>?

Here's an example of what I'm trying to do:

private static readonly Dictionary<string, Dictionary<string, DirectoryInfo[]>[]> OrderTypeToFulfillmentDict = new Dictionary<string, Dictionary<string, DirectoryInfo[]>>()
    {
        {"Type1", new [] 
                { 
                    ProductsInfo.Type1FulfillmentNoSurfacesLocations, 
                    ProductsInfo.Type2FulfillmentSurfacesLocations 
                } 
        }
    }

where Type1Fulfillment..., and Type2Fulfillment... are already constructed as

Dictionary<string, DirectoryInfo[]>. 

This throws the following compiler error:

"Cannot convert from System.Collections.Generic.Dictionary<string, System.IO.DirectoryInfo[]>[] to System.Collections.Generic.Dictionary<string, System.IO.DirectoryInfo[]>"

Edit: The problem was, as Lanorkin pointed out, that I was missing the final [] in the new Dictionary<string, Dictionary<string, DirectoryInfo[]>>(). Still, it goes without saying that this probably isn't something anyone should be trying to do in the first place.

like image 905
furkle Avatar asked Jun 20 '26 04:06

furkle


1 Answers

What you've got looks correct, but what you're doing has a real code smell about it that's going to lead to some serious technical debt.

For starters, rather than having an inner Dictionary<string, string[]> model this in a class with methods appropriate to what you're trying to model. Otherwise anyone accessing this type isn't going to have a clue about what it's really modeling.

like image 131
Sean Avatar answered Jun 21 '26 20:06

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!