
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to remove an item from Set
Firstly, declare a HashSet and add elements −
var names = new HashSet<string>(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin");
To remove an element, use RemoveWhere.
names.RemoveWhere(x => x == "John");
Let us see the complete example −
Example
using System; using System.Collections.Generic; public class Program { public static void Main() { var names = new HashSet < string > (); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin"); Console.WriteLine("Initial Set..."); foreach(var val in names) { Console.WriteLine(val); } names.RemoveWhere(x => x == "John"); Console.WriteLine("Set after removing an element..."); foreach(var val in names) { Console.WriteLine(val); } } }
- Related Articles
- How to remove an item from an ArrayList in C#?
- Remove an item from a Hashtable in C#
- Swift Program to Remove an Element from the Set
- How to remove an item from a C# list by using an index?
- How to remove an item from an ArrayList in Kotlin?
- How to remove an item from JavaScript array by value?
- Java program to remove items from Set
- Python Program to remove items from set
- How to remove an item from a C++ STL vector with a certain value?
- Remove a specified item from SortedSet in C#
- How to remove indentation from an unordered list item using CSS?
- MongoDB query to remove item from array?
- Python Program to remove items from the set
- Swift Program to Remove Null from a Set
- Swift Program to Remove a Subset from a Set

Advertisements