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的基本用法
最新推荐文章于 2023-12-24 09:08:37 发布