本文整理汇总了C#中System.Collections.Hashtable.ContainsValue方法的典型用法代码示例。如果您正苦于以下问题:C# Hashtable.ContainsValue方法的具体用法?C# Hashtable.ContainsValue怎么用?C# Hashtable.ContainsValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Hashtable
的用法示例。
在下文中一共展示了Hashtable.ContainsValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections;
public class SamplesHashtable
{
public static void Main()
{
// Creates and initializes a new Hashtable.
var myHT = new Hashtable();
myHT.Add(0, "zero");
myHT.Add(1, "one");
myHT.Add(2, "two");
myHT.Add(3, "three");
myHT.Add(4, "four");
// Displays the values of the Hashtable.
Console.WriteLine("The Hashtable contains the following values:");
PrintIndexAndKeysAndValues(myHT);
// Searches for a specific key.
int myKey = 2;
Console.WriteLine("The key \"{0}\" is {1}.", myKey, myHT.ContainsKey(myKey) ? "in the Hashtable" : "NOT in the Hashtable");
myKey = 6;
Console.WriteLine("The key \"{0}\" is {1}.", myKey, myHT.ContainsKey(myKey) ? "in the Hashtable" : "NOT in the Hashtable");
// Searches for a specific value.
var myValue = "three";
Console.WriteLine("The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable");
myValue = "nine";
Console.WriteLine("The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable");
}
public static void PrintIndexAndKeysAndValues(Hashtable myHT)
{
int i = 0;
Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
foreach (DictionaryEntry de in myHT)
Console.WriteLine($"\t[{i++}]:\t{de.Key}\t{de.Value}");
Console.WriteLine();
}
}
输出:
The Hashtable contains the following values: -INDEX- -KEY- -VALUE- [0]: 4 four [1]: 3 three [2]: 2 two [3]: 1 one [4]: 0 zero The key "2" is in the Hashtable. The key "6" is NOT in the Hashtable. The value "three" is in the Hashtable. The value "nine" is NOT in the Hashtable.
示例2: Main
//引入命名空间
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
Hashtable myHashtable = new Hashtable();
myHashtable.Add("AL", "Alabama");
myHashtable.Add("CA", "California");
myHashtable.Add("FL", "Florida");
myHashtable.Add("NY", "New York");
myHashtable.Add("WY", "Wyoming");
foreach (string myKey in myHashtable.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
foreach(string myValue in myHashtable.Values)
{
Console.WriteLine("myValue = " + myValue);
}
if (myHashtable.ContainsValue("Florida"))
{
Console.WriteLine("myHashtable contains the value Florida");
}
}
}