Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtable with multiple values for single key

I want to store multiple values in single key like:

HashTable obj = new HashTable();
obj.Add("1", "test");
obj.Add("1", "Test1");

Right now this throws an error.

like image 247
user557168 Avatar asked Dec 29 '10 13:12

user557168


People also ask

Can Hashtable have multiple values for a key?

The array can have any number of values. And, each key can have an array with a different number of values.

Can hash tables have the same key?

You cant do that, All the keys in the Hash map or Hash Table should be unique.

Can Hashtable have duplicate keys C#?

In Hashtable, key must be unique. Duplicate keys are not allowed.

Which is faster Hashtable or dictionary?

Dictionary is a generic type and returns an error if you try to find a key which is not there. The Dictionary collection is faster than Hashtable because there is no boxing and unboxing.


1 Answers

you can put your test,test1,test2,... in a table and then put this table in a Hashtable as a value for the key which will be the same for all them.

For example try something like this:

List<string> list = new List<string>();
list.Add("test");
list.Add("test1"); 

and then:

HashTable obj = new HashTable();
obj.Add("1", list);
like image 188
Grace Avatar answered Oct 18 '22 12:10

Grace