C# 中 Dictionary 和 Hashtable 之间的区别
Hashtable 比 Dictionary 慢。对于强类型集合,Dictionary 集合更快。
Hashtable
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
Dictionary
Dictionary 是 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP