在C#中,实例化和初始化字典的语法是什么,其中包含了一个字典数组,这些字典本身包含数组作为值?
例如,(我相信),
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
}
}
}Type1Fulfillment和Type2Fulfillment..。已经被构造为
Dictionary<string, DirectoryInfo[]>. 这会引发以下编译器错误:
"Cannot convert from System.Collections.Generic.Dictionary<string, System.IO.DirectoryInfo[]>[] to System.Collections.Generic.Dictionary<string, System.IO.DirectoryInfo[]>"编辑:问题是,正如拉诺金所指出的,我错过了新Dictionary<string, Dictionary<string, DirectoryInfo[]>>()中的最后一个Dictionary<string, Dictionary<string, DirectoryInfo[]>>()。尽管如此,不用说,这可能不是任何人一开始就应该尝试去做的事情。
发布于 2014-03-20 16:04:15
你所拥有的看起来是正确的,但是你所做的有一个真正的code smell,它将导致一些严重的technical debt。
首先,与其在类中使用适合您所要建模的方法,不如在类中使用内部Dictionary<string, string[]>模型。否则,任何访问这种类型的人都不会知道它真正的模型是什么。
发布于 2014-03-20 16:04:25
就像这样:
var dic = new Dictionary<string, Dictionary<int, int[]>[]>
{
{
"key1",
new[]
{
new Dictionary<int, int[]>
{
{1, new[] {1, 2, 3, 4}}
}
}}
};发布于 2014-03-20 16:03:24
Dictionary<string, Dictionary<string, string[]>[]> complexDictionary = new Dictionary<string, Dictionary<string, string[]>[]>();或者使用var关键字:
var complexDictionary = new Dictionary<string, Dictionary<string, string[]>[]>();https://stackoverflow.com/questions/22538494
复制相似问题