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.

das-g
  • 9,718
  • 4
  • 38
  • 80
Casey
  • 12,070
  • 18
  • 71
  • 107
  • 4
    You're probably missing a semicolon from the line before it. Maybe you should post more of the code. – Gabe May 08 '10 at 02:23
  • Possible duplicate of [Proper way to initialize a C# dictionary with values already in it?](https://stackoverflow.com/questions/17047602/proper-way-to-initialize-a-c-sharp-dictionary-with-values-already-in-it) – DavidRR Jul 19 '17 at 12:23

5 Answers5

4

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.

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 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. – Casey May 08 '10 at 02:36
  • 1
    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. – Casey May 08 '10 at 02:53
  • Wow. weird glitch in Matrix. :-) – Pretzel May 08 '10 at 03:14
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.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
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...)

Pretzel
  • 8,141
  • 16
  • 59
  • 84
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");
Robert Williams
  • 1,340
  • 9
  • 12
  • 1
    -1 OP specifically asked about the C# 3.0 collection initializer syntax. – Bryan Watts May 08 '10 at 02:32
  • Agree -1. Doesn't answer the question at all. –  May 08 '10 at 02:34
  • 2
    My option was just a "what if" you do it this way instead. This question has already been asked anyways. http://stackoverflow.com/questions/495038/can-i-use-a-collection-initializer-for-dictionarytkey-tvalue-entries – Robert Williams May 08 '10 at 02:48
  • 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 May 08 '10 at 03:00
  • @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. – Bryan Watts May 08 '10 at 16:18
  • @Bryan: Computer/Car analogies are tired, but I'll let you go with it. No, in this case it's more like: I'm trying to do something with this SUV that I'm experimenting with, but it's not working right. And Robert said, hey, how about trying this Pickup Truck instead. It's not as pretty, but it works just as well and it lets you get on with your day. – Pretzel May 08 '10 at 17:01
  • @Pretzel: That analogy could have been made about toasters. Responding to car analogies with other car analogies is tired, but I'll let you go with it. – Bryan Watts May 08 '10 at 19:31
  • @Bryan: You completely missed the flaw in your analogy and instead went for the strawman. The problem with your analogy is that owning a car is both expensive and a commitment (heavy). Using one coding methodology or using another, in this case, is relatively lightweight (both inexpensive and not a heavy commitment.) It costs the coder nothing to try something different, even if it isn't what he thinks he wants. Your analogy falls apart. I still think Robert Williams suggestion isn't worth modding down. It's not bad advice, it's just not the answer the OP was hoping for. But it's still useful. – Pretzel May 10 '10 at 13:59
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?

Sukasa
  • 1,700
  • 4
  • 22
  • 40