c# | HashSet 中的元素个数
一个 HashSet 是一个无序的独特元素的集合。在系统中找到。集合.通用命名空间。它用于我们希望防止在集合中插入重复项的情况。就性能而言,与列表相比更好。可以使用 HashSet 。计数属性 计算哈希表中的元素数量。
语法:
mySet.Count;
这里我自己是 HashSet
下面给出了一些例子,以便更好地理解实现:
例 1:
// C# code to get the number of
// elements that are contained in HashSet
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of integers
HashSet<int> mySet = new HashSet<int>();
// Inserting elements in HashSet
for (int i = 0; i < 5; i++) {
mySet.Add(i * 2);
}
// To get the number of
// elements that are contained in HashSet
Console.WriteLine(mySet.Count);
}
}
Output:
5
例 2:
// C# code to get the number of
// elements that are contained in HashSet
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a HashSet of integers
HashSet<int> mySet = new HashSet<int>();
// To get the number of
// elements that are contained in HashSet.
// Note that, here the HashSet is empty
Console.WriteLine(mySet.Count);
}
}
Output:
0
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处