gpt4 book ai didi

c# - 使用值初始化 C# 字典的正确方法

转载 作者:IT王子 更新时间:2023-10-29 03:27:39 24 4
gpt4 key购买 nike

我正在使用以下代码在 C# 文件中创建字典:

private readonly Dictionary<string, XlFileFormat> FILE_TYPE_DICT
= new Dictionary<string, XlFileFormat>
{
{"csv", XlFileFormat.xlCSV},
{"html", XlFileFormat.xlHtml}
};

new 下有一条红线错误:

Feature 'collection initilializer' cannot be used because it is not part of the ISO-2 C# language specification

这是怎么回事?

我正在使用 .NET 版本 2。

最佳答案

我无法在简单的 .NET 4.0 控制台应用程序中重现此问题:

static class Program
{
static void Main(string[] args)
{
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};

Console.ReadKey();
}
}

你能尝试在一个简单的控制台应用程序中重现它并从那里开始吗?您的目标很可能是 .NET 2.0(不支持它)或客户端配置文件框架,而不是支持初始化语法的 .NET 版本。

关于c# - 使用值初始化 C# 字典的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com