C# 哈希表

WBOY
发布: 2024-09-03 15:04:59
原创
380 人浏览过

C# 中的哈希表是键值对形式的数据集合,键值对基于键的哈希码,键用于访问集合内的元素或数据。它是从Objects类继承到Hashtable的。因此,基本上,C# 或任何编程语言中的哈希表都是键和值对的简单表示,它们以哈希码格式正确组织。

语法:

现在我们知道什么是 C# 哈希表,让我们继续了解正确实现哈希表的标准语法。以下是在程序中使用哈希表的标准语法和所需的系统文件。

using System.Collections;
Hashtableht = new Hashtable();
登录后复制

包含集合的系统文件负责导入哈希表使用的所需函数和方法。那么hashtable就是这里的主要关键字,我们创建了一个实例作为ht,我们的操作将在新创建的ht上执行。现在我们知道了实现哈希表的正确语法,让我们了解它是如何工作的。

哈希表在 C# 中如何工作?

如前所述,我们知道哈希表是键值对形式的数据或信息的集合。键值对的一个简单示例是“Name:Sulaksh”,这里键是Name,值是Sulaksh,键保持不变,而值可以不同。 Hashtable 由 key 和 value 组成,用大括号表示,并使用哈希函数进行计算。

现在让我们正确实现哈希表并了解示例的工作原理。

C# 哈希表示例

我们的第一个示例是哈希表的简单实现,其中我们有一个简单的哈希表,带有数字键和值,我们将打印哈希表中的元素总数,代码如下:

示例#1

代码:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n The Total No. of elements: {0}", exampleht.Count);
}
}
登录后复制

代码说明:从系统文件开始,这里集合是最重要的,然后是我们的类,其中是我们的 main 方法。在 main 方法中,我们声明了一个哈希表,后跟三个键值对。我们已经实现了 add 函数来插入元素。因此,我们的哈希表由三个键值对组成,最后,我们有一个打印语句,它将打印哈希表中的元素总数,即三个。我们在这里使用一个简单的计数函数,请参考下面所附的输出屏幕截图:

C# 哈希表

正如预期的那样,输出告诉我们哈希表中有四个元素,现在转到下一个示例,我们将尝试显示哈希表的键和值。

示例#2

代码:

using System;
using System.Collections;
class exampleHT {
static publicvoid Main() {
HashtablesampleHT = new Hashtable();
sampleHT.Add(1, " One");
sampleHT.Add(2, " Two");
sampleHT.Add(3, " Three");
Console.WriteLine("\n Below is the content of Hashtable: \n");
foreach (DictionaryEntry entry in sampleHT) {
Console.WriteLine(" {0}, {1}", entry.Key, entry.Value);
}
}
}
登录后复制

代码说明:与前面的示例类似,我们有系统文件和类,其中包含 main 方法。然后我们有哈希表,后面是键值对,然后是打印语句。然后我们有 foreach 语句,它将一次选择一个元素,并在下一行中将其打印为输出。我们的输出预计是键和值形式的元素列表,请参阅下面的屏幕截图。

C# 哈希表

正如预期的那样,输出打印了哈希表的元素,现在在下一个示例中,我们将使用 outhashtable 实现清除函数,代码如下。

示例#3

代码:

using System;
using System.Collections;
class sampleht {
static public void Main()  {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n Elements of the Hashtable :");
foreach(DictionaryEntry ele1 in exampleht) {
Console.WriteLine(ele1.Value);
}
Console.WriteLine(" No. of elements before clearing: {0} ", exampleht.Count);
exampleht.Clear();
Console.WriteLine(" Total No. of elements now: {0} ", exampleht.Count);
}
}
登录后复制

代码说明:通过我们所需的系统文件和方法,我们定义了哈希表和总共三个键值对。我们已经实现了 add 函数来将元素添加到哈希表中,然后我们的 print 语句将简单地打印哈希表中的所有元素,即三个。在清除哈希表之前,我们已经打印了列表中存在的元素总数,然后我们有clear函数,它将清除整个哈希表,这意味着每个元素都将从列表中清除,最后的打印语句将打印数字现在存在的元素数量将为零。

C# 哈希表

正如所解释的,clear 函数完成了它的工作,并且列表被清除,现在,继续,对于我们的下一个示例,我们将实现删除函数。

示例#4

代码:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n List of Original Elements:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
exampleht.Remove(3);
exampleht.Remove(1);
Console.WriteLine("\n Elements after removal:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
}
}
登录后复制

Code Explanation: Just as our earlier examples, we have our system files, class, and the main method. Then we have our hashtable with a total of three key values. Then we have our print statement, which will print out the original list along with key and values, using foreach. We then have the remove function for two keys, which will remove the keys that we pass, then we have our print statement, which will use foreach and print every single element present in the hashtable after removing.

C# 哈希表

Advantages

Advantages of any function or methodology is important to understand its real time applications and hashtable’s most widely known advantage is how it allows the developer the ability for synchronization. Hashtable has noted advantages over the search tree and this makes them practical to use in real time computer applications and for the purpose of database indexing. Speed, compared to other table type data structures is best delivered by hashtables. Having key value pairs makes it easy to anticipate the format regarding data and restructuring is done easily.

Conclusion

Hashtable in C# is a collection of elements, represented in a key value pair format. The key can be the same, while values differ and the key cannot be null while a value can be. We implemented an example of functions like remove, clear, and print. Hashtable has a speed advantage over other data structures.

以上是C# 哈希表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!