<< Click to Display Table of Contents >> Collections - Hashtable |
![]() ![]() ![]() |
using System;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// com strings
Hashtable h = new Hashtable();
h.Add("Flavio", "Cris");
h.Add("Fera", "Bela");
h.Add("Han", "Leia");
Response.Write(h["Flavio"].ToString() + "<br>");
if (!h.ContainsKey("Solo"))
Response.Write("Não contém Solo<br>");
// listando conteúdo!
IDictionaryEnumerator en = h.GetEnumerator();
while (en.MoveNext())
{
Console.WriteLine(en.Key + "=" + en.Value);
}
// com objetos
h.Clear();
Pessoa p1 = new Pessoa() { Nome = "Kaylee", Profissao = "Mecânica" };
Pessoa p2 = new Pessoa() { Nome = "Wash", Profissao = "Piloto" };
Pessoa p3 = new Pessoa() { Nome = "Inara", Profissao = "Acompanhante" };
Endereco e1 = new Endereco() { Cidade = "Criciuma", Pais = "Brasil" };
Endereco e2 = new Endereco() { Cidade = "Nova York", Pais = "EUA" };
Endereco e3 = new Endereco() { Cidade = "Tokio", Pais = "Japão" };
h.Add(p1, e1);
h.Add(p2, e2);
h.Add(p3, e3);
Endereco ende = (Endereco)h[p2];
Response.Write(ende.Cidade + "," + ende.Pais + "<br>");
if (h.ContainsValue(e2))
Response.Write("Contém e2<br>");
}
}