C#中的字典和哈希表之间的区别
哈希表比字典慢。对于强类型的集合,Dictionary集合更快。
哈希表
Hashtable类表示键和值对的集合,这些键和值对基于键的哈希码进行组织。它使用键来访问集合中的元素。
让我们看一个例子-
示例
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("E001", "Tom");
ht.Add("E098", "Amit");
ht.Add("E110", "Jack");
ICollection key = ht.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + ht[k]);
}
Console.ReadKey();
}
}
}输出结果
E001: Tom E098: Amit E110: Jack
字典
字典是C#中键和值的集合。Dictionary<TKey,TValue>包含在System.Collection.Generics命名空间中。
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, int> dict = new Dictionary<int, int>();
dict.Add(1,234);
dict.Add(2,489);
dict.Add(3,599);
dict.Add(4,798);
dict.Add(5,810);
dict.Add(6,897);
dict.Add(7,909);
Console.WriteLine("Dictionary elements: "+dict.Count);
}
}输出结果
Dictionary elements: 7
热门推荐
10 日语送考祝福语简短
11 20岁简短生日祝福语
12 打牌翻盘祝福语简短
13 职场离职文案祝福语简短
14 买房的祝福语高级简短
15 美丽晚霞祝福语简短句
16 佛教生日祝福语简短大全
17 女生成年祝福语简短
18 新疆新年祝福语大全简短