C # hash table (Hashtable)
Hashtable class represents a series of organized based on the hash code keykey / value pairs.The key is used to access elements of the collection.
When you use thekey to access theelements, use a hash table, and you can identify a useful key. Each entry in the hash table has akey / value pairs.Key is used to access the items in the collection.
Hashtable class methods and properties
The following table lists some of the commonattributes Hashtableclass:
属性 | 描述 |
---|---|
Count | 获取 Hashtable 中包含的键值对个数。 |
IsFixedSize | 获取一个值,表示 Hashtable 是否具有固定大小。 |
IsReadOnly | 获取一个值,表示 Hashtable 是否只读。 |
Item | 获取或设置与指定的键相关的值。 |
Keys | 获取一个 ICollection,包含 Hashtable 中的键。 |
Values | 获取一个 ICollection,包含 Hashtable 中的值。 |
The following table lists some of the commonmethods of the Hashtableclass:
序号 | 方法名 & 描述 |
---|---|
1 | public virtual void Add(
object key,
object value
);
向 Hashtable 添加一个带有指定的键和值的元素。 |
2 | public virtual void Clear();
从 Hashtable 中移除所有的元素。 |
3 | public virtual bool ContainsKey(
object key
);
判断 Hashtable 是否包含指定的键。 |
4 | public virtual bool ContainsValue(
object value
);
判断 Hashtable 是否包含指定的值。 |
5 | public virtual void Remove(
object key
);
从 Hashtable 中移除带有指定的键的元素。 |
Examples
The following example demonstrates the concept of a hash table (Hashtable) of:
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main (string [] args) { Hashtable ht = new Hashtable (); ht.Add ( "001", "Zara Ali"); ht.Add ( "002", "Abida Rehman"); ht.Add ( "003", "Joe Holzner"); ht.Add ( "004", "Mausam Benazir Nur"); ht.Add ( "005", "M. Amlan"); ht.Add ( "006", "M. Arif"); ht.Add ( "007", "Ritesh Saikia"); if (ht.ContainsValue ( "Nuha Ali")) { Console.WriteLine ( "This student name is already in the list"); } else { ht.Add ( "008", "Nuha Ali"); } // Get the collection of keys ICollection key = ht.Keys; foreach (string k in key) { Console.WriteLine (k + ":" + ht [k]); } Console.ReadKey (); } } }
When the above code is compiled and executed, it produces the following results:
001: Zara Ali 002: Abida Rehman 003: Joe Holzner 004: Mausam Benazir Nur 005: M. Amlan 006: M. Arif 007: Ritesh Saikia 008: Nuha Ali