您不能使用定义类字段var
.
Change var
to Dictionary<string, Dictionary<string, string>>
:
public Dictionary<string, Dictionary<string, string>> info =
new Dictionary<string, Dictionary<string, string>>
{
{
"Gen",
new Dictionary<string, string>
{
{"name", "Genesis"},
{"chapters", "50"},
{"before", ""},
{"after", "Exod"}
}
},
{
"Exod",
new Dictionary<string, string>
{
{"name", "Exodus"},
{"chapters", "40"},
{"before", "Gen"},
{"after", "Lev"}
}
}
};
See here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables欲了解更多信息var
关键字及其用法。
在 C# 9 中,引入了目标类型的新表达式 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new,您可以将字段定义重写为:
public Dictionary<string, Dictionary<string, string>> info = new()
{
{
"Gen",
new Dictionary<string, string>
{
{"name", "Genesis"},
{"chapters", "50"},
{"before", ""},
{"after", "Exod"}
}
},
{
"Exod",
new Dictionary<string, string>
{
{"name", "Exodus"},
{"chapters", "40"},
{"before", "Gen"},
{"after", "Lev"}
}
}
};