码迷,mamicode.com
首页 > Windows程序 > 详细

C#中Dictionary的初始化方式

时间:2020-06-03 20:45:29      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:ems   view   version   specific   syn   lock   itil   引入   iss   

本文翻译自:Proper way to initialize a C# dictionary with values?

I am creating a dictionary in a C# file with the following code: 我正在使用以下代码在C#文件中创建字典:

  1. private readonly Dictionary<string, XlFileFormat> FILE_TYPE_DICT
  2. = new Dictionary<string, XlFileFormat>
  3. {
  4. {"csv", XlFileFormat.xlCSV},
  5. {"html", XlFileFormat.xlHtml}
  6. };

There is a red line under new with the error: new错误下面有一条红线:

Feature ‘collection initilializer‘ cannot be used because it is not part of the ISO-2 C# language specification 无法使用功能‘集合initilializer‘,因为它不是ISO-2 C#语言规范的一部分

Can anyone explain what is going on here? 谁能解释一下这里发生了什么?

EDIT: okay, so it turns out I was using .NET version 2. 编辑:好的,所以事实证明我使用的是.NET版本2。


#1楼

参考:https://stackoom.com/question/19WrK/使用值初始化C-字典的正确方法


#2楼

I can‘t reproduce this issue in a simple .NET 4.0 console application: 我无法在简单的.NET 4.0控制台应用程序中重现此问题:

  1. static class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var myDict = new Dictionary<string, string>
  6. {
  7. { "key1", "value1" },
  8. { "key2", "value2" }
  9. };
  10. Console.ReadKey();
  11. }
  12. }

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. 您似乎可能是针对.NET 2.0(不支持它)或客户端配置文件框架,而不是支持初始化语法的.NET版本。


#3楼

Object initializers were introduced in C# 3.0, check which framework version you are targeting. 在C#3.0中引入了对象初始化程序,检查您要定位的框架版本。

Overview of C# 3.0 C#3.0概述


#4楼

With C# 6.0, you can create a dictionary in following way: 使用C#6.0,您可以通过以下方式创建字典:

  1. var dict = new Dictionary<string, int>
  2. {
  3. ["one"] = 1,
  4. ["two"] = 2,
  5. ["three"] = 3
  6. };

It even works with custom types. 它甚至适用于自定义类型。


#5楼

You can initialize a Dictionary (and other collections) inline. 您可以内联初始化Dictionary (和其他集合)。 Each member is contained with braces: 每个成员都包含大括号:

  1. Dictionary<int, StudentName> students = new Dictionary<int, StudentName>
  2. {
  3. { 111, new StudentName { FirstName = "Sachin", LastName = "Karnik", ID = 211 } },
  4. { 112, new StudentName { FirstName = "Dina", LastName = "Salimzianova", ID = 317 } },
  5. { 113, new StudentName { FirstName = "Andy", LastName = "Ruth", ID = 198 } }
  6. };

See MSDN for details. 有关详细信息,请参阅MSDN


#6楼

Suppose we have a dictionary like this 假设我们有这样的字典

  1. Dictionary<int,string> dict = new Dictionary<int, string>();
  2. dict.Add(1, "Mohan");
  3. dict.Add(2, "Kishor");
  4. dict.Add(3, "Pankaj");
  5. dict.Add(4, "Jeetu");

We can initialize it as follow. 我们可以按如下方式初始化它。

  1. Dictionary<int, string> dict = new Dictionary<int, string>
  2. {
  3. { 1, "Mohan" },
  4. { 2, "Kishor" },
  5. { 3, "Pankaj" },
  6. { 4, "Jeetu" }
  7. };

C#中Dictionary的初始化方式

标签:ems   view   version   specific   syn   lock   itil   引入   iss   

原文地址:https://www.cnblogs.com/sunny3158/p/13039640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!