furkle
furkle

Reputation: 5059

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

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.

Upvotes: 3

Views: 165

Answers (5)

Grax32
Grax32

Reputation: 4059

 private static readonly Dictionary<string, Dictionary<string, DirectoryInfo[]>> 

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

You have the wrong type in your variable definition. Remove the final "[]" as you don't want an array of dictionaries.

Upvotes: 0

Dan Hunex
Dan Hunex

Reputation: 5318

The following is perfectly valid

     // array of dictionary
     Dictionary<int, string[]>[] matrix = new Dictionary<int, string[]>[4];

//Dictionary of string and dictionary array
 Dictionary<string, Dictionary<string, string[]>[]> dicOfArrays= new Dictionary<string, Dictionary<string, string[]>[]>();

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

Something like this:

var dic = new Dictionary<string, Dictionary<int, int[]>[]>
        {
            {
                "key1", 
                new[]
                {
                    new Dictionary<int, int[]>
                    {
                        {1, new[] {1, 2, 3, 4}}
                    }
                }}
        };

Upvotes: 2

Sean
Sean

Reputation: 62502

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.

Upvotes: 5

Abbas
Abbas

Reputation: 14432

Dictionary<string, Dictionary<string, string[]>[]> complexDictionary = new Dictionary<string, Dictionary<string, string[]>[]>();

or using the var keyword:

var complexDictionary = new Dictionary<string, Dictionary<string, string[]>[]>();

Upvotes: 1

Related Questions