C#: HashTable Genérico

Publicado por Marcos Dell Antonio em 19/03/2007 | .NET

Achei estranho não existir a coleção genérica HashTable. Porém, existe uma equivalente: Dictionary<TKey, TValue>.

Para usá-la é o mesmo estilo da HashTable:

Dictionary<string, int> lista = new Dictionary<string, int>(); lista.Add("numero1", 1); lista.Add("numero2", 2); foreach (KeyValuePair<string, int> item in lista) { Console.Write("Key = " + item.Key + " - Value = " + item.Value); }

É isso ae. Até +.

2 comentários

  1. 1
    Cristina // August 30th, 2007 at 5:42 pm

    Marco,

    Tem HashTable sim!
    Mas é necessário adicionar o

    using System.Collections;

    E não é necessário dizer que é de ’string’ e ‘int’

    Exemplo:
    ———–

    Hashtable meuHash = new Hashtable();

    meuHash.Add( “numero1″, 1);
    meuHash.Add( “numero2″, 2);

    []´s

    Cristina

  2. 2
    Marcos // August 31st, 2007 at 8:13 am

    Cristina,

    Mas essa sua solução utiliza o HashTable não-genérico. Veja no método Add que ele recebe objects.

    T+.

Deixe o seu comentário

Anúncios

Anúncio provido pelo BuscaPé