如何在 C# 中创建 HashTable 集合?

PHPz
PHPz 转载
2023-08-31 23:13:02 529浏览

如何在 C# 中创建 HashTable 集合?

HashTable是 C# 中的非泛型集合。它存储键值对,类似于通用的“字典”集合。 HashTable 定义在

System. Collections. namespace.

HashTable计算每个键的哈希码并将其存储在内部不同的桶中。然后,当访问值时,哈希码将与指定键的哈希码进行匹配。因此,查找通过 HashTable 进行了优化。

在本教程中,我们将了解如何在 C# 中创建 HashTable 集合。

哈希表功能

在我们开始创建 HashTable 之前,让我们看看 C# 中 HashTable 集合的一些显着功能。

HashTable 集合存储键值对。

哈希表是系统的一部分。 C# 中的集合命名空间并实现 IDictionary 接口。 HashTable 的元素存储为 DictionaryEntry 对象。

哈希表的键不能为空并且是唯一的。但是,值可以为空或重复。

可以使用索引器中的键来访问哈希表中的值,就像访问数组值一样。

HashTable 中的键是不可变的对象。这些关键对象中的每一个都提供了一个哈希函数。

典型的 Hashtable 类实现 C# 的 IDictionary、ICollection、ISerializable、IEnumerable、IDeserializationCallback 和 ICloneable 接口。

HashTable中存储的元素可以是相同类型,也可以是不同类型。

牢记这些显着特征,现在让我们讨论如何在 C# 中创建哈希表。

如何在 C# 中创建 HashTable 集合?

C#的HashTable类提供了16个重载构造函数来创建HashTable。

下表显示了我们将在本文中使用的 HashTable 构造函数。

构造函数 描述
哈希表() 初始化新的、具有默认初始容量、哈希码提供程序、比较器和负载因子的 HashTable 类的空实例。
哈希表(IDictionary) 创建 Hashtable 类的新实例并使用指定字典的内容对其进行初始化。

注意- 要了解有关 C# 中 HashTable 类的更多信息,请阅读我们的文章 C#-HashTable 类。

让我们看看在 C# 中创建 HashTable 集合通常遵循的步骤。

首先,我们包括 System.我们程序中的集合命名空间

using System. Collections;

接下来,我们使用 Hashtable 类创建一个哈希表。为此,我们使用默认构造函数。

Hashtable hashtable_name = new Hashtable();

现在我们可以使用“Add()”方法将元素添加到HashTable中。

所以这里,我们可以在创建HashTable实例的同时初始化整个HashTable,也可以使用Add()方法将元素一一添加到HashTable中。

示例 1

以下程序演示了用 C# 创建 HashTable。

using System;
using System. Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create hashtable using the default constructor
      Hashtable indianNumberSystem = new Hashtable();
      
      //add a key/value pair using the Add() method
      indianNumberSystem.Add(1,"Ones"); 
      indianNumberSystem.Add(10,"Tens");
      indianNumberSystem.Add(100,"Hundred");
      indianNumberSystem.Add(1000,"Thousand");
      indianNumberSystem.Add(10000,"Ten Thousand");
      indianNumberSystem.Add(100000,"Lac");
      indianNumberSystem.Add(1000000,"Ten Lac");
      indianNumberSystem.Add(10000000,"Crore");
      
      //display HashTable contents
      Console.WriteLine("Key, Value pairs from Indian Number System:");
      foreach(DictionaryEntry ele1 in indianNumberSystem){
         Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
      }      
   }
}

在上面的程序中,我们使用默认构造函数定义了一个 HashTable 实例。然后我们使用 Add()方法将键/值对添加到HashTable中。最后,使用 for-each 循环将 HashTable 内容一一打印出来。

输出

上述程序生成以下输出。

Key, Value pairs from Indian Number System:
100 (Hundred) 
1000 (Thousand) 
10 (Tens) 
1000000 (Ten Lac) 
100000 (Lac) 
10000000 (Crore) 
10000 (Ten Thousand) 
1 (Ones)

程序显示一个包含印度数字系统的位值的哈希表。请注意,由于这是一个创建哈希表并向其中添加内容的简单程序,因此输出未格式化。

示例 2

让我们再举一个在 C# 中创建 HashTable 的例子。以下程序使用不同的构造函数来创建 HashTable。

using System;
using System.Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create hashtable without using Add method
      Hashtable my_hashtable1 = new Hashtable() {{"K1", "New York"}};

      // Adding key/value pair in the hashtable using Add() method
      my_hashtable1.Add("K2", "Paris");
      my_hashtable1.Add("K3", "London");
      my_hashtable1.Add("K4", "Mumbai");
      my_hashtable1.Add("K5", "Berlin");
      
      Console.WriteLine("Key, Value pairs from my_hashtable1:");
      foreach(DictionaryEntry ele1 in my_hashtable1){
         Console.WriteLine("{0} and {1} ", ele1.Key, ele1.Value);
      }      
   }
}

正如我们在上面的代码中看到的,首先我们创建一个具有一个键值对的 HashTable 对象。然后我们使用HashTable类的add()方法向HashTable添加元素。最后,使用 for-each 循环,迭代 HashTable 对象以打印每个 hashTable 元素(键值对)。

输出

上面的程序产生以下输出。

Key, Value pairs from my_hashtable1:
K2 and Paris 
K1 and New York 
K3 and London 
K4 and Mumbai 
K5 and Berlin 

在上面的输出中,键值对按值的字母顺序逆序显示。这是哈希表的默认输出,因为我们没有提供任何代码来格式化输出。 HashTable 类提供了各种方法来组织/格式化输出,我们将在后续教程中学习这些方法。

在本教程中,我们讨论了如何在 C# 中创建 HashTable 集合。 HashTable 是键值对的非通用集合。 HashTable 中的键是唯一的非空值。值可以为空和重复。我们可以使用Systems提供的HashTable类在C#中创建HashTable。集合接口并使用此类提供的各种方法对其进行修改。

以上就是如何在 C# 中创建 HashTable 集合?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除