3

I'm having trouble with the following collections initialization:

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

I keep getting various compiler errors about the syntax. From what I have googled, this should be perfectly valid C# 3.0 code.

The first error that pops up is:

Error   102 ; expecte

What am I doing wrong?

Update

The line that it keeps telling me it expects the ; at is right after the closing ) parenthesis.

I am attempting to create this inside of a static class. If I just remove this, than everything compiles fine.

2

5 Answers 5

3

My conclusion: You're targeting framework 2.0, otherwise it's impossible to get that error.

Are you 101% sure you're using the framework 3.0?

EDIT

1 - On the Project (Solution Explorer), right click on ProjectName and go to Properties.

2 - Check on the Application tab the item "Target Framework"

If says 2.0 change it to 3.0 and do the same for all the projects on the solution.

Sign up to request clarification or add additional context in comments.

3 Comments

This is weird, I was working on converting this app to 3.5 from 2.0, but than realized that the server I'm deploying too only supports 3.0. So I went back to 3.0. If I build the solution, I have no problems. If I build the project, it gives me errors. If I do a rebuild of the project right after, it also succeeds. I'm using VS2008 Professional.
Only have a single solution and a single project. I went back to 3.5 cleaned and rebuilt everything and than back to 3.0 and did the same and the problem seemed to have dissapeared.
Wow. weird glitch in Matrix. :-)
1

Are you making it a property/field? You marked it with Private?

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

This is a local variable Dictionary called temp

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

This is a local member.

Both of these compile, is there more code you are leaving out?

I would just start a new class and slowly put in fields, properties, etc. . until you figure out the error. The code you put up is perfectly legal when I am using VS 2008. If you recreate your class slowly it might show you where your error actually is.

1 Comment

+1 The incorrect syntax is probably somewhere else in the file.
0

What if you just declare the dictionary and then add your key/value pairs.

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

8 Comments

-1 OP specifically asked about the C# 3.0 collection initializer syntax.
Agree -1. Doesn't answer the question at all.
My option was just a "what if" you do it this way instead. This question has already been asked anyways. stackoverflow.com/questions/495038/…
Srsly. Just because someone steps outside of the scope of the question, doesn't mean it's a bad response. In fact, it's sometimes necessary to helpfully guide a n00b. It's a perfectly good suggestion in my book.
@Pretzel This answer is akin to the OP asking about a problem in their Honda engine and @Robert Williams responding "Buy American!". American cars may well be a viable option, but in the context of that question, they are irrelevant.
|
0

Look for any potential errors above that line; I copied your code into a new project in Visual Studio and it compiled without any errors. What are you defining this dictionary as a part of?

Comments

0

I think you need to get rid of the () (corrected by David Basarab. Thanks David!)

This page shows some good examples: http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c11987

EDIT

Ok, so I finally launched VS2008 and had to give this a try myself.

I pasted the OP's code right into a WinForms project, right in the main "public Form1()" method and hit the Build button and it wouldn't compile.

Delete the "private" keyword and it Compiles just fine.

(I tried using public, internal, etc. and none of those would compile either...)

3 Comments

The () are optional you can include them or not.
Hmmm... Good to know. I'll remove my suggestion, but I still think the link to the article is a good start for him.
I couldn't test it in C# 3, but I did in C# 4. I think that you don't need the () in C# 3. In C# 4, it works correctly with and without the ().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.