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.
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.
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.
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.
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.
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"}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With