📜  C#|从HashSet中删除指定的元素

📅  最后修改于: 2021-05-29 23:14:59             🧑  作者: Mango

HashSet是唯一元素的无序集合。它位于System.Collections.Generic命名空间下。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。 HashSet .Remove(T)方法用于从HashSet 对象中删除指定的元素。

句法:

public bool Remove (T item);

这里, item是要删除的元素。

返回值:如果成功找到并删除了该元素,则该方法返回True;如果在HashSet >对象中未找到该项目,则返回False。

以下示例说明了HashSet的用法.Remove(T)方法

范例1:

// C# code to remove the specified 
// element from a HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of integers
        HashSet mySet = new HashSet();
  
        // Inserting even numbers less than
        // equal to 20 in HashSet mySet
        for (int i = 0; i < 10; i++) {
            mySet.Add(i * 2);
        }
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
  
        // Removing the element 10 if present
        if (mySet.Contains(10)) {
            mySet.Remove(10);
        }
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
}
输出:
The elements in HashSet are : 
0
2
4
6
8
10
12
14
16
18
Number of elements are : 10
The elements in HashSet are : 
0
2
4
6
8
12
14
16
18
Number of elements are : 9

范例2:

// C# code to remove the specified
//  element from a HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of strings
        HashSet mySet = new HashSet();
  
        // Inserting elements into HashSet
        mySet.Add("Data Structures");
        mySet.Add("Algorithms");
        mySet.Add("Java");
        mySet.Add("Puzzles");
        mySet.Add("Coding");
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(string i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
  
        // Removing the element "JavaScript" if present
        if (mySet.Contains("JavaScript")) {
            mySet.Remove("JavaScript");
        }
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(string i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
}
输出:
The elements in HashSet are : 
Data Structures
Algorithms
Java
Puzzles
Coding
Number of elements are : 5
The elements in HashSet are : 
Data Structures
Algorithms
Java
Puzzles
Coding
Number of elements are : 5

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.remove?view=netframework-4.7.2