检查HashSet是否是C#中指定集合的子集
要检查HashSet是否是指定集合的子集,代码如下-
示例
using System;using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<string> set1 = new HashSet<string>();
set1.Add("EF");
set1.Add("OP");
Console.WriteLine("Elements in HashSet1");
foreach(string val in set1){
Console.WriteLine(val);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("KL");
set2.Add("MN");
set2.Add("OP");
set2.Add("QR");
Console.WriteLine("Elements in HashSet2");
foreach(string val in set2){
Console.WriteLine(val);
}
Console.WriteLine("Is set1 a subset of set2? "+set1.IsSubsetOf(set2));
}
}
输出结果
这将产生以下输出-
Elements in HashSet1EF
OP
Elements in HashSet2
KL
MN
OP
QR
Is set1 a subset of set2? False
示例
让我们看另一个例子-
using System;using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<string> set1 = new HashSet<string>();
set1.Add("EF");
set1.Add("OP");
Console.WriteLine("Elements in HashSet1");
foreach(string val in set1){
Console.WriteLine(val);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("EF");
set2.Add("OP");
set2.Add("QR");
Console.WriteLine("Elements in HashSet2");
foreach(string val in set2){
Console.WriteLine(val);
}
Console.WriteLine("Is set1 a subset of set2? "+set1.IsSubsetOf(set2));
}
}
输出结果
这将产生以下输出-
Elements in HashSet1EF
OP
Elements in HashSet2
EF
OP
QR
Is set1 a subset of set2? True
以上是 检查HashSet是否是C#中指定集合的子集 的全部内容, 来源链接: utcz.com/z/322529.html