How to fill an array of hashtable objects in C # (VS2008)?

0

I need to create an array that contains n hashtable () objects or that contains n objects. I do not know how to declare this array or arrayList in C # (VS2008)

Hashtable myHT = new Hashtable();
Array myArray = // ¿ Como declararlo ? 
    
asked by Popularfan 13.09.2018 в 12:43
source

1 answer

2

To work with arrays, you should do it in the following way

//Defino un array de Hastable con 10 posiciones 
Hashtable[] array = new Hashtable[10];

//Lo recorro e inicializo cada objeto dentro dle array
for(int i = 0;i < array.Length ; i++)
{
   array[i] = new Hashtable();
}

And you can work with your array of Hashtable , however, if you do not have very specific requirements, it would be better to work with List<T> since it is much more practical.

An example would be

List<Hashtable> Lista = new List<Hashtable>();

Hashtable hashTable = new Hashtable();

//Trabajas con el objeto y luego podes añadirlo a la lista, sin necesidad de definir un tamaño
Lista.add(hashTable);

Edit

If you're looking to use an Array instead of a list (and you do not know what size it will be), you can do the following

//Definis una lista
List<Hashtable> Lista = new List<Hashtable>(); 
Hashtable hashTable = new Hashtable();
//La llenas según necesidad
Lista.add(hashTable);
//Definis el array le asignas la lista utilizando ToArray()
Hashtable[] hashTableArray = Lista.ToArray();
    
answered by 13.09.2018 / 13:39
source