1.55. C# hashtable

发布时间 : 2025-10-25 13:34:40 UTC      

Page Views: 10 views

The Hashtable class represents a series of key/value pairs organized by key-based hash code. It uses keys to access elements in the collection.

When you use keys to access elements, a hash table is used, and you can identify a useful key value. Each item in the hash table has a key/value pair. Key is used to access items in the collection.

1.55.1. Methods and properties of the Hashtable class #

The following table lists some common properties of Hashtable class:

Attribute

Description

Count

Gets the number of key-value pairs contained in Hashtable.

IsFixedSize

Gets a value indicating whether the Hashtable has a fixed size.

IsReadOnly

Gets a value indicating whether the Hashtable is read-only.

Item

Gets or sets the value associated with the specified key.

Keys

Gets an ICollection containing the keys in the Hashtable.

Values

Gets an ICollection that contains the value in the Hashtable.

The following table lists some common methods of Hashtable :

Serial number

Method name & description

1

Public virtual void Add (object key, object value); adds an element with thespecified key and value to Hashtable.

2

Public virtual void Clear (); removes all elements from the Hashtable.

3

Public virtual bool ContainsKey (object key); determines whether the Hashtable contains the specified key.

4

Public virtual bool ContainsValue (object value); determines whether the Hashtable contains the specified value.

5

Public virtual void Remove (object key); removes the element with the specified key from the Hashtable.

Example #

The following example demonstrates the concept of a Hashtable:

1.55.2. Example #

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 a 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:

007: Ritesh Saikia 004: Mausam Benazir Nur 005: M. Amlan 008: Nuha Ali 002: Abida Rehman 003: Joe Holzner 001: Zara Ali 006: M. Arif 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。