C# 程序检查哈希表中是否存在值

2023年 8月 29日 80.5k 0

C# 程序检查哈希表中是否存在值

The hashtable is an organized collection of key-value pairs wherein the keys are arranged as per the hash code of the key calculated using the hash function. While the keys should be non-null and unique in a hashtable, the values can be null and duplicate.

哈希表中的元素是通过键来访问的。在C#中,类"Hashtable"表示哈希表集合。这个类提供了各种属性和方法,我们可以使用它们来执行操作并访问哈希表中的数据。

在本文中,我们将看到如何确定哈希表中是否存在特定的值。

How to Check if Value Exists in Hashtable?

要检查哈希表中是否存在某个值,我们可以利用Hashtable类提供的“containsValue”方法。该方法返回一个布尔值,指示指定的值是否存在于哈希表中。

Let’s have a look at the method first before proceeding with programming examples.

ContainsValue方法

Syntax − public virtual bool ContainsValue (object value);

Description − used to find if the Hashtable contains a specified value.

参数 - 要在哈希表中定位的值(对象)。可以是空值。

返回值 − Boolean: true=> 哈希表中包含具有指定值的元素。

False=> 哈希表不包含指定值的元素。

命名空间 - System.Collections

Let’s now see few programming examples where we check if the specified value is present in the hashtable or not.

Example

的中文翻译为:

示例

检查值是否存在于哈希表中的第一个程序如下所示。

using System;
using System.Collections;
class Program {
public static void Main(){
// Create a Hashtable
Hashtable langCodes = new Hashtable();

// Add elements to the Hashtable
langCodes.Add("C++", "CPlusPlus");
langCodes.Add("C#", "CSharp");
langCodes.Add("Java", "Java");
langCodes.Add("PL", "Perl");

// use ContainsValue method to check if the HashTable contains the
//required Value or not.
if (langCodes.ContainsValue("CSharp"))
Console.WriteLine("langCodes hashtable contain the Value = CSharp");
else
Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
}
}

登录后复制

上述程序声明了一个langCodes哈希表,其中包含语言代码和语言名称作为键和值。接下来,我们有一个“if”结构,检查哈希表中是否存在值“CSharp”。如果存在,它将相应地显示消息。

输出

程序的输出如下所示。

langCodes hashtable contain the Value = CSharp

登录后复制

由于hashtable中存在value = CSharp,所以程序会显示上述消息。

Now change the argument of the ContainsValue method to “C#” i.e. the key instead of value.

if (langCodes.ContainsValue("C#"))

登录后复制

现在用这个更改来执行上面的程序。

输出

在这种情况下,由于哈希表中不存在值“C#”,程序将返回适当的消息。因此,我们将得到−

langCodes hashtable doesn't contain the Value = CSharp

登录后复制

Example

的中文翻译为:

示例

Now let’s look at the following example.

using System;
using System.Collections;
class Program {
public static void Main() {
// Create a Hashtable
Hashtable NumberNames = new Hashtable();
// Add elements to the Hashtable
NumberNames.Add(1, "One");
NumberNames.Add(3, "Three");
NumberNames.Add(5, "Five");
NumberNames.Add(7, "Seven");
// use ContainsValue method to check if the HashTable contains the
//required Value or not.
if (NumberNames.ContainsValue("Two"))
Console.WriteLine("NumberNames hashtable contain the Value = Two");
else
Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
if (NumberNames.ContainsValue("Five"))
Console.WriteLine("NumberNames hashtable contain the Value = Five");
else
Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
}
}

登录后复制

这个程序有一个名为“NumberNames”的表,以数字作为键,对应的名称作为值。在这里,我们首先使用“containsKey()”方法检查哈希表是否包含值= Two。接下来,我们使用containsKey()方法检查值=“Five”。

Output

The output for the program is shown below.

NumberNames hashtable doesn't contain the Value = Two
NumberNames hashtable contain the Value = Five

登录后复制

从程序中定义的哈希表中可以看出,它不包含值 = Two,但包含值 = Five。因此,程序适当地给出了相应的消息。

结论

Thus using the “containsKey()” method of the Hashtable class in C#, we can determine if an element with a specific value is present in the hashtable or not. Depending on whether the value is present or not, we can output the appropriate results, or in the case of a complex program, proceed with the appropriate code.

当我们需要检查hashtable中是否存在指定的值,并采取相应的行动时,方法“containsKey()”就变得非常有用。

以上就是C# 程序检查哈希表中是否存在值的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论