Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dictionary with arrays as values

Tags:

c#

dictionary

I'm trying to initialize a dictionary with string elements as keys and int[] elements as values, as follows:

System.Collections.Generic.Dictionary<string,int[]> myDictionary;
myDictionary = new Dictionary<string,int[]>{{"length",{1,1}},{"width",{1,1}}};

But the debugger keeps saying: "Unexpected symbol '{'".

Could you tell me what's wrong with the above code?

Thank you!

like image 438
Jodll Avatar asked Mar 31 '12 22:03

Jodll


People also ask

Can a dictionary value be an array?

A Dictionary object contains a set of key-item pairs. A dictionary's item is a value that is unambiguously associated with a unique key and the key is used to retrieve the item. The key can be of any type except a variant or an array (but generally it is a string or still an integer).


1 Answers

I am not sure for c# but the following works in Java for example:

instead of

{1,1}

try

new int[]{1,1}

or

new[]{1,1}
like image 75
kasavbere Avatar answered Sep 30 '22 01:09

kasavbere