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”。
outputthe output for the program is shown below.
numbernames hashtable doesn't contain the value = twonumbernames 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# 程序检查哈希表中是否存在值的详细内容。