enenkey
enenkey

Reputation: 1271

Declaration of a hashtable with key, value

I'm in C# 2.0.

I would like to know if it is possible to declare a Hashtable const initiated with key & values. I know that it is possible with arrays:

public  static string[] ColumnsNames = 
{ "string1", "string2", "string3", "string4"
, "string5", "string6", "string7" };

but how can we do that with Hashtables.

Upvotes: 5

Views: 21884

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1503779

It's easy to create a static readonly field with C# 3 collection initializers, which can still target .NET 2. Using a Dictionary<string, string> instead of a Hashtable though (don't use the nongeneric collections unless you really have to1).

It isn't a compile-time constant that you can declare with const, but I'm hoping you weren't really after const.

private static readonly Dictionary<string, string> Foo
    = new Dictionary<string, string>
{
    { "Foo", "Bar" },
    { "Key", "Value" },
    { "Something", "Else" }
};

There's nothing similar in C# 2, if you really have to use that. Are you really still using Visual Studio 2005? Don't forget that you can still target .NET 2 with C# 3 and 4...

EDIT: If you really want to do it with C# 2 and hashtables, you could write a static method like this:

public static Hashtable CreateHashtable(params object[] keysAndValues)
{
    if ((keysAndValues.Length % 2) != 0)
    {
        throw new ArgumentException("Must have an even number of keys/values");
    }
    Hashtable ret = new Hashtable();
    for (int i = 0; i < keysAndValues.Length; i += 2)
    {
        ret[keysAndValues[i]] = keysAndValues[i + 1];
    }
    return ret;
}

Then:

private static readonly Hashtable Foo = HashtableHelper.CreateHashtable(
    "key1", "value1", "key2", 10, "key3", 50);

I'd really not recommend that though...


1 The same syntax will work with Hashtable if you're using C# 3, but it's really, really worth using the generic collections if you possibly can.

Upvotes: 8

Stefan Georgiev
Stefan Georgiev

Reputation: 139

You should be using a Dictionary. On average the Hastable is about 1/3 slower than the Dictionary (mainly due to implementation). Further it is obsolete.

Upvotes: 0

Yahia
Yahia

Reputation: 70369

You mean

Dictionary<string, string> DDD = new Dictionary<string, string> { { "A", "B" }, { "X", "Y" }, { "Z", "A" } };

Upvotes: 1

Sir Rippov the Maple
Sir Rippov the Maple

Reputation: 7581

It cannot be done in C# 2.0. The language does not support it. The language specification is here and there is no mention of inline dictionary initialisation.

C# 3.0 does allow dictionary initialisation similar to the array initialisation you described in your question (language spec here). Here is an example:

var dictionary = new Dictionary<string, string> {
    {"key1", "value1"},
    {"key2", "value2"}
}; 

Upvotes: 11

Ian
Ian

Reputation: 34549

Believe this should work.

public Hashtable hash = new Hashtable()
{
  { "string1", 1 },
  { "string2", 2 }
}

Upvotes: 8

Related Questions