patterncsharpCritical
Proper way to initialize a C# dictionary with values
Viewed 0 times
withinitializeproperwaydictionaryvalues
Problem
I am creating a dictionary in a C# file with the following code:
There is a red line under
Feature 'collection initilializer' cannot be used because it is not part of the ISO-2 C# language specification
What is going on here?
I am using .NET version 2.
private readonly Dictionary FILE_TYPE_DICT
= new Dictionary
{
{"csv", XlFileFormat.xlCSV},
{"html", XlFileFormat.xlHtml}
};There is a red line under
new with the error:Feature 'collection initilializer' cannot be used because it is not part of the ISO-2 C# language specification
What is going on here?
I am using .NET version 2.
Solution
I can't reproduce this issue in a simple .NET 4.0 console application:
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.
static class Program
{
static void Main(string[] args)
{
var myDict = new Dictionary
{
{ "key1", "value1" },
{ "key2", "value2" }
};
Console.ReadKey();
}
}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.
Code Snippets
static class Program
{
static void Main(string[] args)
{
var myDict = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
Console.ReadKey();
}
}Context
Stack Overflow Q#17047602, score: 1246
Revisions (0)
No revisions yet.