检查一个哈希表是否等于C#中的另一个哈希表
要检查一个Hashtable是否等于另一个Hashtable,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash1 = new Hashtable();
hash1.Add("1", "Kevin");
hash1.Add("2", "Steve");
hash1.Add("3", "Tim");
hash1.Add("4", "Gary");
hash1.Add("5", "Kevin");
hash1.Add("6", "Steve");
hash1.Add("7", "Tom");
hash1.Add("8", "Stephen");
Console.WriteLine("HashSet1...");
ICollection key = hash1.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + hash1[k]);
}
Hashtable hash2 = new Hashtable();
hash2.Add("1", "Kevin");
hash2.Add("2", "Steve");
hash2.Add("3", "John");
hash2.Add("4", "Tim");
key = hash2.Keys;
Console.WriteLine("\nHashSet2...");
foreach (string k in key) {
Console.WriteLine(k + ": " + hash2[k]);
}
Console.WriteLine("\nAre both the Hashtable equal? "+(hash1.Equals(hash2)));
}
}输出结果
这将产生以下输出-
HashSet1... 1: Kevin 2: Steve 3: Tim 4: Gary 5: Kevin 6: Steve 7: Tom 8: Stephen HashSet2... 1: Kevin 2: Steve 3: John 4: Tim Are both the Hashtable equal? False
示例
让我们看另一个例子-
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash1 = new Hashtable();
hash1.Add("1", "Kevin");
hash1.Add("2", "Steve");
hash1.Add("3", "John");
hash1.Add("4", "Tim");
Console.WriteLine("HashSet1...");
ICollection key = hash1.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + hash1[k]);
}
Hashtable hash2 = new Hashtable();
hash2.Add("1", "Nathan");
hash2.Add("2", "Gary");
hash2.Add("3", "John");
hash2.Add("4", "Tim");
hash2.Add("5", "Steve");
ICollection key2 = hash2.Keys;
Console.WriteLine("\nHashSet2...");
foreach (string k in key2) {
Console.WriteLine(k + ": " + hash2[k]);
}
Console.WriteLine("\nAre both the Hashtable equal? "+(hash1.Equals(hash2)));
}
}输出结果
这将产生以下输出-
HashSet1... 1: Kevin 2: Steve 3: John 4: Tim HashSet2... 1: Nathan 2: Gary 3: John 4: Tim 5: Steve Are both the Hashtable equal? False
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿