using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
//提供快速的查詢。元素的存儲與順序無關。不能在指定位置插入元素,
//因爲它本身沒有有效的排序。感覺它的優點體現在查詢上。
// hashtable的鍵必須是唯一的,沒有有效的排序,它進行的是內在的排序
namespace ConsoleApplication25
{
class penson
{
public string name;
}
class Program
{
static void Main(string[] args)
{
Hashtable hash = new Hashtable();
hash.Add("ch", "cheng");//hashtable是以鍵值對存在。給hashtable添加值
hash.Add("cs", new penson { name = "lishi" });
Console.WriteLine(hash["ch"]);
penson p = hash["cs"] as penson;
Console.WriteLine(p.name);
//判斷某個鍵是否存在
if (hash.ContainsKey("cs"))
{
Console.WriteLine("存在");
}
//遍歷hashtable的兩種方式。
foreach (DictionaryEntry item in hash)
{
Console.WriteLine("鍵是{0},值是{1}", item.Key, item.Value);
}
foreach(object items in hash.Keys)
{
Console.WriteLine("鍵是{0},值是{1}",items,hash[items]);
}
Console.ReadLine();
}
}
}
C#hashtable的基本用法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.