Lee Loftiss
Lee Loftiss

Reputation: 3195

c# fill hashtable when defining

Is it possible to fill a hashtable when it is first defined?

For example, here is some similar PHP code:

var $AA = new array("one" => 1, "two" => 2);

I would like to do something like this in C#. In fact, it is quite possible Hashtable is not the best tool.

take care, lee

Upvotes: 1

Views: 191

Answers (1)

Matt Stephenson
Matt Stephenson

Reputation: 8620

Look into the collection initializers - yes, it's possible. For dictionaries, see here. You can declare your dictionary and fill it with a collection of key/value pairs.

A simpler example than MSDN provides:

var MyDictionary = new Dictionary<string,int>()
{
    {"one", 1},
    {"two", 2}
};

Upvotes: 6

Related Questions