简体   繁体   中英

how store in hash table in c#?

I want to store array or linked list in hash table EX:

       array [,] m = new array(){"doucument",13.4}; 
       hashtable h = new hashtable();
       h.add(1,m );

please help me how store this array in hashtable,and If I can, how access to element this array? Error: A nested array initializer is expected in line 1

                     array [,] m = new array(){"doucument",13.4};
  var dictionary = new Dictionary<int, object[]>();
  dictionary.Add(1, new object[] {"doucument",13.4});

  var arr = dictionary[1];

from your question it wasn't obvious if this is a key-value collection or just values. Just to throw it in there, you should know that with .NET 3.5 and up, you can use a HashSet, example:

HashSet<int> numbers = new HashSet<int>();

Just get your syntax errors out of your code and it will work fine:

       using System.Collections;

       Array[,] m = new Array[,]{"document",13.4}; 
       Hashtable h = new Hashtable();
       h.Add(1,m );

Accessing the stored element:

       m = (Array[,])h[1]; 

Of course, personally I would prefer the use of System.Collections.Generic.Dictionary ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM