C#|检查HashSet和指定集合是否包含相同的元素

A. 哈希集 是一个无序的 独特元素 .它属于 系统收藏。通用的 名称空间。它用于我们希望防止在集合中插入重复项的情况。就性能而言,它比列表要好。 哈希集 .SetEquals(IEnumerable) )方法 用于检查哈希集和指定集合是否包含相同的元素。

null

语法:

mySet1.SetEquals(mySet2);

在这里 mySet1 mySet2 是哈希集对象。

返回类型: 此方法返回 符合事实的 如果mySet1等于mySet2,则返回 错误的 .

例外情况: 这种方法会给你 无理例外 如果HashSet是 无效的 .

下面给出了一些例子,以更好地理解实施:

例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< string > mySet1 = new HashSet< string >();
// Inserting elements in HashSet
mySet1.Add( "Geeks" );
mySet1.Add( "GeeksforGeeks" );
mySet1.Add( "GeeksClasses" );
mySet1.Add( "GeeksQuiz" );
// Creating a HashSet of strings
HashSet< string > mySet2 = new HashSet< string >();
// 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< int > mySet1 = new HashSet< int >();
// Inserting elements in HashSet
mySet1.Add(4);
mySet1.Add(8);
mySet1.Add(12);
mySet1.Add(16);
// Creating a HashSet of integers
HashSet< int > mySet2 = new HashSet< int >();
// 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

参考:

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享