这个C#字典初始化如何正确?
我偶然发现了以下内容,我想知道为什么它没有引起语法错误这个C#字典初始化如何正确?,c#,dictionary,initialization,C#,Dictionary,Initialization,我偶然发现了以下内容,我想知道为什么它没有引起语法错误 var dict = new Dictionary<string, object> { ["Id"] = Guid.NewGuid(), ["Tribes"] = new List<int> { 4, 5 }, ["MyA"] = new Dictionary<string, object> { ["Name"] = "Solo", ["Po
var dict = new Dictionary<string, object>
{
["Id"] = Guid.NewGuid(),
["Tribes"] = new List<int> { 4, 5 },
["MyA"] = new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
}
["OtherAs"] = new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
}
};
var dict=新字典
{
[“Id”]=Guid.NewGuid(),
[“部落”]=新名单{4,5},
[“MyA”]=新词典
{
[“Name”]=“Solo”,
[“点数”]=88
}
[“OtherAs”]=新列表
{
新词典
{
[“点数”]=1999
}
}
};
注意,“MyA”和“OtherAs”之间缺少“,”
这就是混淆的地方:
为什么这不违法?这是故意的吗?缺少逗号会产生很大的不同。它使索引器
[“OtherAs”]
应用于此词典:
new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
}
以下是您的代码,但已重新格式化,并添加了一些括号,以说明编译器是如何对其进行解析的:
["MyA"]
=
(
(
new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
}["OtherAs"]
)
=
(
new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
}
)
)
[“MyA”]
=
(
(
新词典
{
[“Name”]=“Solo”,
[“点数”]=88
}[“其他人”]
)
=
(
新名单
{
新词典
{
[“点数”]=1999
}
}
)
)
这里发生的事情是,您正在创建一个字典,然后在其中建立索引。然后返回索引器/赋值表达式的结果,这就是分配到MyA
字典槽中的结果
这:
[“MyA”]=新词典
{
[“Name”]=“Solo”,
[“点数”]=“88”
}
[“OtherAs”]=新列表
{
新词典
{
[“点数”]=1999
}
}
可拆分为以下psuedo代码:
var temp = new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
};
// indexed contains result of assignment
var indexed = temp["OtherAs"] = new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
};
// value is set to result of assignment from previous step
["MyA"] = indexed;
// temp is discarded
var temp=新字典
{
[“Name”]=“Solo”,
[“点数”]=88
};
//索引包含赋值结果
var index=temp[“OtherAs”]=新列表
{
新词典
{
[“点数”]=1999
}
};
//值设置为上一步的赋值结果
[“MyA”]=索引;
//温度被丢弃
将返回分配给第二个字典的索引器的结果(分配返回分配的值/右侧),该字典是一个临时本地字典,只是“消失在以太中”。索引器(字典列表)的结果是最终放入主字典的内容
这是一种奇怪的情况,由于使用了
object
作为字典值的类型,因此更容易陷入这种情况 @Steve他就是这么问的。我将它粘贴到sharplab中,它看起来像最初的OtherAs
被添加为MyA
字典的一个键。(Name=Solo,Points=88加上OtherAs=List)除非它从未分配它。相反,它将DICT列表放置在MyA
槽中,该槽中只包含single Points=1999条目,覆盖了人们认为属于该槽的内容。链接太长,无法粘贴到第一条注释中。这的确很奇怪。是的,我用LinqPad测试了它,得到了相同的结果。不知道这里发生了什么。让我们看看一些C#guru是否可以在这里提供一些帮助。如果您将第一个字典更改为,并将Points修改为“88”`则会出现一个编译器错误“无法隐式转换type”System.Collections.Generic.List'为'string',这实际上帮助我找到了答案@OlegI我不认为这是一个编译器错误。答案中已经提供了许多很好的解释。但是如果您尝试var test=newdictionary{[“Name”]=“Solo”,“Points”]=88}[“OtherAs”]=newlist{newdictionary{[“Points”]=1999}
将进一步解释。正是将字典的值类型从对象
更改为字符串
,给了我类似的错误消息和引发答案的aha时刻。看来你赢了我--我责怪你在手机上键入答案
["MyA"]
=
(
(
new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
}["OtherAs"]
)
=
(
new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
}
)
)
["MyA"] = new Dictionary<string, string>
{
["Name"] = "Solo",
["Points"] = "88"
}
["OtherAs"] = new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
}
var temp = new Dictionary<string, object>
{
["Name"] = "Solo",
["Points"] = 88
};
// indexed contains result of assignment
var indexed = temp["OtherAs"] = new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
["Points"] = 1999
}
};
// value is set to result of assignment from previous step
["MyA"] = indexed;
// temp is discarded