Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialise a New System.Collections.Generic.Dictionary with String key/value pairs?

Tags:

Is it possible to create and initialise a System.Collections.Generic.Dictionary object with String key/value pairs in one statement?

I'm thinking along the lines of the constructor for an array of Strings..

e.g.

Private mStringArray As String() = {"String1", "String2", "etc"} 

In case this is turns out to be a syntactic sugar kind of thing, I'd prefer an answer that I can use in .Net 2.0 (Visual Studio 2005), and Visual Basic - though I'm curious if it's possible at all so don't let that put you off ;o)

like image 481
Andrew Avatar asked Nov 25 '08 19:11

Andrew


People also ask

How do you initialize a new dictionary?

Dictionaries are also initialized using the curly braces {} , and the key-value pairs are declared using the key:value syntax. You can also initialize an empty dictionary by using the in-built dict function. Empty dictionaries can also be initialized by simply using empty curly braces.

Is dictionary generic collection in C#?

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of Dictionary is, it is generic type.


1 Answers

Like this:

Dim myDic As New Dictionary(Of String, String) From {{"1", "One"}, {"2", "Two"}} 
like image 84
jgauffin Avatar answered Oct 15 '22 14:10

jgauffin