C# 哈希表

Hashtable 是一个非泛型集合,它存储键/值对,这些键/值对根据每个键的哈希码进行排列。

Hashtable 类实现了 ICollection

C# Hashtable implements ICollection
C# Hashtable 实现 ICollection

创建 Hashtable

要在 C# 中创建 Hashtable,我们需要使用 System.Collections 命名空间。以下是我们在 C# 中创建 Hashtable 的方法。

// create a hashtable
Hashtable myTable = new Hashtable();

示例:创建 Hashtable

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
// create a hashtable Hashtable myTable = new Hashtable();
// add items to hashtable myTable.Add("Name", "Ginny"); myTable.Add("RollNumber", 12); myTable.Add("Address", "Miami"); // print value of the element whose key is "RollNumber" Console.WriteLine(myTable["RollNumber"]); } }

输出

12

Hashtable 的基本操作

在 C# 中,我们可以在 hashtable 上执行不同的操作。在本教程中,我们将介绍一些常用的 Hashtable 操作。

  • 添加元素
  • 访问元素
  • 更改元素
  • 移除元素

让我们更详细地看看如何执行这些操作!


在 Hashtable 中添加元素

C# 提供了 Add() 方法,我们可以使用它在 Hashtable 中添加元素。例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable
        Hashtable myTable = new Hashtable();

// add items to hashtable myTable.Add("Subject", "Math"); myTable.Add("Code", 139);
} }

在上面的示例中,我们创建了一个名为 myTableHashtable

这里我们使用 Add() 方法添加了键/值对。

  • 键 - "Subject""Code"
  • 值 - "Math"139

注意:Hashtable 中,键必须唯一且不能为 null。但是,值可以为 null 或重复。


向 Hashtable 添加元素的另一种方法

在不使用 Add() 方法的情况下向 Hashtable 添加元素

我们可以在不使用 Add() 方法的情况下添加 Hashtable 元素。例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
// create a hashtable and add elements Hashtable myTable = new Hashtable() { { 1, "Programiz" }, { "Greet", "Hello" } };
} }

这也称为集合初始化器。


访问 Hashtable 元素

我们可以使用键来访问 Hashtable 中的元素。例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable and add elements 
        Hashtable myTable = new Hashtable();

        // add items to hashtable
        myTable.Add("Employee", "James");
        myTable.Add("Id", 13);

// access the value whose key is "Employee" Console.WriteLine(myTable["Employee"]);
// access the value whose key is "Id" Console.WriteLine(myTable["Id"]);
} }

输出

James
13

在上面的示例中,我们使用键访问了 Hashtable 的值。

  • myTable["Employee"] - 访问键为 "Employee" 的值
  • myTable["Id"] - 访问键为 "Id" 的值

遍历 Hashtable

在 C# 中,我们也可以使用 foreach 循环遍历 Hashtable 的每个元素。例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable and add elements 
        Hashtable myTable = new Hashtable();

        // add items to hashtable
        myTable.Add("Employee", "Jake");
        myTable.Add("Id", 23);
        myTable.Add("Address", "Cornelia Street");

// print keys of hashtable foreach (var item in myTable.Keys) { Console.WriteLine("Key = {0}", item); }
// print values of hashtable foreach (var item in myTable.Values) { Console.WriteLine("Value = {0}", item); }
} }

输出

Key = Address
Key = Employee
Key = Id
Value = Cornelia Street
Value = Jake
Value = 23

在上面的示例中,我们使用 foreach 循环遍历了 myTable


遍历 Hashtable 的另一种方法

使用 DictionaryEntry 遍历

Hashtable 的元素存储在 DictionaryEntry 中,因此我们可以使用 DictionaryEntry 来遍历 Hashtable。例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable and add elements 
        Hashtable myTable = new Hashtable();

        // add items to hashtable
        myTable.Add("Employee", "Jake");
        myTable.Add("Id", 23);
        myTable.Add("Address", "Cornelia Street");

// iterate using DictionaryEntry foreach (DictionaryEntry item in myTable) { Console.WriteLine("{0} : {1} ", item.Key, item.Value); }
} }

输出

Employee : Jake 
Address : Cornelia Street 
Id : 23 

更改 Hashtable 元素

我们可以像这样更改 Hashtable 中元素的值:

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable and add elements 
        Hashtable myTable = new Hashtable();

        // add items to hashtable
        myTable.Add("Employee", "Jake");
        myTable.Add("Id", 23);
        myTable.Add("Address", "Cornelia Street");

        // print original value 
        Console.WriteLine("Value of Address before changing: " + myTable["Address"]);

// change the value of "Address" key to "Ontario" myTable["Address"] = "Ontario";
// print new updated value of "Address" Console.WriteLine("Value of Address after changing: " + myTable["Address"]); } }

输出

Value of Address before changing: Cornelia Street
Value of Address after changing: Ontario

在这里,我们更改了 myTable"Address" 键的值。


删除 Hashtable 元素

要删除 Hashtable 的元素,我们使用:

  • Remove(key) - 根据指定的键删除元素

例如,

using System;
using System.Collections;
class Program
{
    public static void Main()
    {
        // create a hashtable and add elements 
        Hashtable myTable = new Hashtable();

        // add items to hashtable
        myTable.Add("Employee", "Tom");
        myTable.Add("Id", 5);
        myTable.Add("Address", "London");

        Console.WriteLine("Original Hashtable :");

        foreach (DictionaryEntry item in myTable)
        {
            Console.WriteLine("{0} : {1} ", item.Key, item.Value);
        }

// remove value with key "Id" myTable.Remove("Id");
Console.WriteLine("\nModified Hashtable :"); // iterate through the modified hashtable foreach (DictionaryEntry item in myTable) { Console.WriteLine("{0} : {1} ", item.Key, item.Value); } } }

输出

Original Hashtable :
Employee : Tom 
Address : London
Id : 5

Modified Hashtable :
Employee : Tom
Address : London

在上面的示例中,我们删除了键为 "Id" 的元素。

这里,myTable.Remove("Id")myTable 中删除了 5。因此,当我们遍历 myTable 时,我们会得到一个修改后的 Hashtable,其中不包含 "Id"

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战