简体   繁体   中英

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.

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.

Something like this:

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

或使用var关键字:

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

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[]>[]>();
 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM