HashSet是唯一元素的无序集合。它位于System.Collections.Generic命名空间下。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。哈希集
句法:
mySet1.SetEquals(mySet2);
在这里, mySet1和mySet2是HashSets对象。
返回类型:如果mySet1等于mySet2,则此方法返回True ,否则返回False 。
异常:如果HashSet为null,则此方法将提供ArgumentNullException 。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to check if HashSet and the specified
// collection contain the same elements.
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("GeeksforGeeks");
mySet1.Add("GeeksClasses");
mySet1.Add("GeeksQuiz");
// Creating a HashSet of strings
HashSet mySet2 = new HashSet();
// Inserting elements in HashSet
mySet2.Add("Geeks");
mySet2.Add("GeeksforGeeks");
mySet2.Add("GeeksClasses");
mySet2.Add("GeeksQuiz");
// Check if both HashSets contains same elements
Console.WriteLine(mySet1.SetEquals(mySet2));
}
}
输出:
True
范例2:
// C# code to check if HashSet and the specified
// collection contain the same elements.
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(5);
mySet2.Add(10);
mySet2.Add(15);
mySet2.Add(20);
// Check if both HashSets contains same elements
Console.WriteLine(mySet1.SetEquals(mySet2));
}
}
输出:
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.setequals?view=netframework-4.7.2