Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 819
enenkey Avatar asked Jul 19 '11 12:07

enenkey


People also ask

What is key and value in hash table?

Hashtable stores key/value pair in hash table. In Hashtable we specify an object that is used as a key, and the value we want to associate to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

How do you check if a Hashtable contains a key?

Hashtable containsKey() Method in Java Hashtable. containsKey() method is used to check whether a particular key is present in the Hashtable or not. It takes the key element as a parameter and returns True if that element is present in the table.

What is hash table with example?

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.

How do I find the value of a hash table?

Hashtable. containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Hashtable. It takes the Value as a parameter and returns True if that value is mapped by any of the keys in the table.


1 Answers

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"}
}; 
like image 178
Sir Rippov the Maple Avatar answered Sep 30 '22 22:09

Sir Rippov the Maple