本文整理匯總了C#中System.Collections.Generic.HashSet<T>.Remove方法的典型用法代碼示例。如果您正苦於以下問題:C# HashSet<T>.Remove方法的具體用法?C# HashSet<T>.Remove怎麽用?C# HashSet<T>.Remove使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Generic.HashSet<T>
的用法示例。
在下文中一共展示了HashSet<T>.Remove方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: for
HashSet<int> numbers = new HashSet<int>();
for (int i = 0; i < 20; i++) {
numbers.Add(i);
}
// Display all the numbers in the hash table.
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
// Remove all odd numbers.
numbers.RemoveWhere(IsOdd);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
// Check if the hash table contains 0 and, if so, remove it.
if (numbers.Contains(0)) {
numbers.Remove(0);
}
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);
bool IsOdd(int i)
{
return ((i % 2) == 1);
}
void DisplaySet(HashSet<int> set)
{
Console.Write("{");
foreach (int i in set)
Console.Write(" {0}", i);
Console.WriteLine(" }");
}
輸出:
numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 } numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 } numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }