正确的方法来初始化与在它已经值的C#字典? [英] Proper way to initialize a C# dictionary with values already in it?

查看:116
本文介绍了正确的方法来初始化与在它已经值的C#字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建与下面的代码C#文件的字典:

I am creating a dictionary in a C# file with the following code:

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

有是下了红线与错误:

功能'收集initilializer'不能使用,因为它不是ISO-2 C#语言规范的一部分

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

谁能解释一下是怎么回事

Can anyone explain what is going on here?

编辑:好了,原来我用的是.NET版本2。

okay, so it turns out I was using .NET version 2.

推荐答案

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

I can't reproduce this issue in a simple .NET 4.0 console application:

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

        Console.ReadKey();
    }
}



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

Can you try to reproduce it in a simple Console application and go from there? It seems likely that you're targeting .NET 2.0 (which doesn't support it) or client profile framework, rather than a version of .NET that supports initialization syntax.

这篇关于正确的方法来初始化与在它已经值的C#字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆