Dictionary initialization in C # 3.0

I am having problems with the following initialization of collections:

private Dictionary<string, string> mydictionary = new Dictionary<string, string>()
{
    {"key", "value"}
    , {"key2", "value2"}
    , {"key3", "value3"}
};

I keep getting various compiler errors in the syntax. From what I was looking at googled, this should be the perfectly correct C # 3.0 code.

First pop-up error:

Error   102 ; expecte

What am I doing wrong?

Update

The line in which he continues to tell me expects ;at to be immediately after closing ).

I am trying to create this inside a static class. If I just delete this, everything compiles fine.

+3
source share
5 answers

My conclusion: you are aiming at the framework 2.0, otherwise it is impossible to get this error.

Are you 101% sure that you are using the 3.0 framework?

EDIT

1 - ( ) ProjectName "".

     

2 - "" " "

     

2.0, 3.0 .

+4

/? Private?

Dictionary<string, string> temp = new Dictionary<string, string>()
    {
        { "key", "value" },
        { "key1", "value" },
        { "key2", "value2" }
    };

temp

private Dictionary<string, string> _fieldDictionary = new Dictionary<string, string>()
        {
            { "key", "value" },
            { "key1", "value" },
            { "key2", "value2" }
        };

.

, , ?

, .. . , , , VS 2008. , , .

+1

, () ( David Basarab. , !)

: http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c11987

, , , VS2008 .

OP WinForms " Form1()" "", .

"private", .

( public, internal .., ...)

0

, , /.

  Dictionary<string, string> temp = new Dictionary<string, string>();
    temp.Add("key", "value");
    temp.Add("key1", "value1");
    temp.Add("key2", "value2");
0

Look for any potential errors above this line; I copied your code to a new project in Visual Studio and compiled without any errors. What do you define in this dictionary as part?

0
source

Source: https://habr.com/ru/post/1744581/


All Articles