Initializing a dictionary

M

moo

Forgive the fairly mundane question, but all the code samples I've
found searching for initializing a dictionary have only shown one way,
specifically:

<code>
HybridDictionary toy = new HybridDictionary();
toy.add(key, value);
</code>

In my application I want to create a dictionary where I already know
the initial values that I want to add to the dictionary and was
wondering if theres an analogous 1-line way (ie <code> int[] myNums
{1,2,3,4,5}; </code> ) of doing this. I come from a Python background
where it's fairly simple to say <code> myDict = { "key" : "value",
"key2": "value2"} </code> so I'm used to a bit of syntactic
sugar...any of this in C#?

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

moo,

The syntax for this in C# won't be available until C# 3.0/.NET 3.5
(currently in beta). It will allow you to do something like this:

HybridDictionary toy = new HybridDictionary() { { 1, "toy 1"}, {2, "toy
2"}};

The syntax might be slightly off, but basically, the compiler sees that
and expands it to call the Add method. I believe the condition is that
there has to be an Add method and the collection has to implement
IEnumerable.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
The syntax for this in C# won't be available until C# 3.0/.NET 3.5
(currently in beta). It will allow you to do something like this:

HybridDictionary toy = new HybridDictionary() { { 1, "toy 1"}, {2, "toy
2"}};

The syntax might be slightly off, but basically, the compiler sees that
and expands it to call the Add method. I believe the condition is that
there has to be an Add method and the collection has to implement
IEnumerable.

I think it's slightly uglier than that - you've got to specify
KeyValuePair each time, e.g.

Dictionary<int, string> x = new Dictionary<int, string>
{
new KeyValuePair<int,string>(1, "toy 1"),
new KeyValuePair<int,string>(2, "toy 2")
};

(This doesn't actually compile with the Orcas 1 beta, but I believe it
should. Dictionary<TKey,TValue> implements
ICollection<KeyValuePair<TKey,TValue>> which is what the spec
requires.)

I don't *think* (looking at the May 2006 spec) that there's any short-
cut to specifying the key/value pair. If there is, I'd be very happy to
be corrected.
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
I don't *think* (looking at the May 2006 spec) that there's any short-
cut to specifying the key/value pair. If there is, I'd be very happy to
be corrected.

I'll correct myself: Nick's syntax compiles fine:

Dictionary<int, string> x = new Dictionary<int, string>
{
{1, "toy 1"},
{2, "toy 2"}
};

Compiles and does the right thing under Orcas beta 1.

Unfortunately, I can't find where this is specified anywhere at the
moment. I'm looking into it.
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
Unfortunately, I can't find where this is specified anywhere at the
moment. I'm looking into it.

Just had word back from Eric Lippert - it's a change since the May 2006
spec. The later version of the spec hasn't been released yet.

For discussion of this particular change, see:

http://blogs.msdn.com/madst/archive/2006/10/10/What-is-a-collection_
3F00_.aspx

(And yes, I suspect I may be talking to myself at this point.)
 
N

Nicholas Paldino [.NET/C# MVP]

I actually feel a little bad, as I was going to mention the spec hasn't
been updated, but I knew you would figure it out eventually haha.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
I actually feel a little bad, as I was going to mention the spec hasn't
been updated, but I knew you would figure it out eventually haha.

No, it was helpful - it meant I've found out a load of other bits that
have changed too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top