HashSet是唯一元素的无序集合。它位于System.Collections.Generic命名空间下。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。 HashSet
句法:
mySet1.IsProperSubsetOf(mySet2);
在这里,mySet1和mySet2是两个HashSets 。
返回值:如果我的设定1是mySet2的真子集,否则返回false此方法返回true。
异常:如果HashSets为null,则此方法将提供ArgumentNullException 。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to Check if a HashSet is a proper
// subset of the specified collection
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of strings
HashSet mySet1 = new HashSet();
// Inserting elements in HashSet
mySet1.Add("Geeks");
mySet1.Add("GeeksQuiz");
// Creating a HashSet of strings
HashSet mySet2 = new HashSet();
// Inserting elements in HashSet
mySet2.Add("DS");
mySet2.Add("C++");
mySet2.Add("Java");
mySet2.Add("JavaScript");
mySet2.Add("GeeksQuiz");
mySet2.Add("Geeks");
// Check if a HashSet is a proper
// subset of the specified collection
Console.WriteLine(mySet1.IsProperSubsetOf(mySet2));
}
}
输出:
True
范例2:
// C# code to Check if a HashSet is a proper
// subset of the specified collection
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of integers
HashSet mySet1 = new HashSet();
// Inserting elements in HashSet
mySet1.Add(4);
mySet1.Add(8);
mySet1.Add(12);
mySet1.Add(16);
// Creating a HashSet of integers
HashSet mySet2 = new HashSet();
// Inserting elements in HashSet
mySet2.Add(4);
mySet2.Add(8);
mySet2.Add(15);
mySet2.Add(20);
// Check if a HashSet is a proper
// subset of the specified collection
Console.WriteLine(mySet1.IsProperSubsetOf(mySet2));
}
}
输出:
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.ispropersubsetof?view=netframework-4.7.2