Introduction to Sets
A set is a collection type that stores unique elements only—no duplicates are allowed. In C#, the most common implementation is HashSet<T>, where T is the type of elements you want to store. This makes sets especially useful when you need to eliminate duplicates or quickly check if an item already exists.
To use a HashSet<T>, you declare it by specifying the type of elements in angle brackets. For example, to store integers, you write HashSet<int>. You can add elements using the Add method, and if you try to add a duplicate, the set will simply ignore it. This built-in duplicate prevention is what makes sets different from lists or arrays, which allow repeated values.
Program.cs
12345678910111213141516171819202122232425using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> numbers = new HashSet<int>(); numbers.Add(10); numbers.Add(20); numbers.Add(30); numbers.Add(10); // Duplicate, will be ignored Console.WriteLine("Numbers in the set:"); foreach (int number in numbers) { Console.WriteLine(number); } } } }
When you add elements to a HashSet<T>, each value is checked for uniqueness. If the value already exists, it is not added again. To check if a value exists, use the Contains method. To remove a value, use the Remove method. Here is a step-by-step breakdown:
- Create a new
HashSet<T>by specifying the type in angle brackets; - Add elements using the
Addmethod; - Use
Containsto check if an element is present; - Remove elements with the
Removemethod.
This workflow allows you to manage a collection of unique items efficiently.
Program.cs
123456789101112131415161718192021222324252627282930using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 }; HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 }; setA.UnionWith(setB); Console.WriteLine("After UnionWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } setA = new HashSet<int> { 1, 2, 3, 4 }; setA.IntersectWith(setB); Console.WriteLine("After IntersectWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } } } }
1. What is the main feature of a HashSet?
2. Which method checks if a value exists in a HashSet?
3. Fill in the blanks to add elements to a HashSet<string>.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Großartig!
Completion Rate verbessert auf 4.76
Introduction to Sets
Swipe um das Menü anzuzeigen
A set is a collection type that stores unique elements only—no duplicates are allowed. In C#, the most common implementation is HashSet<T>, where T is the type of elements you want to store. This makes sets especially useful when you need to eliminate duplicates or quickly check if an item already exists.
To use a HashSet<T>, you declare it by specifying the type of elements in angle brackets. For example, to store integers, you write HashSet<int>. You can add elements using the Add method, and if you try to add a duplicate, the set will simply ignore it. This built-in duplicate prevention is what makes sets different from lists or arrays, which allow repeated values.
Program.cs
12345678910111213141516171819202122232425using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> numbers = new HashSet<int>(); numbers.Add(10); numbers.Add(20); numbers.Add(30); numbers.Add(10); // Duplicate, will be ignored Console.WriteLine("Numbers in the set:"); foreach (int number in numbers) { Console.WriteLine(number); } } } }
When you add elements to a HashSet<T>, each value is checked for uniqueness. If the value already exists, it is not added again. To check if a value exists, use the Contains method. To remove a value, use the Remove method. Here is a step-by-step breakdown:
- Create a new
HashSet<T>by specifying the type in angle brackets; - Add elements using the
Addmethod; - Use
Containsto check if an element is present; - Remove elements with the
Removemethod.
This workflow allows you to manage a collection of unique items efficiently.
Program.cs
123456789101112131415161718192021222324252627282930using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 }; HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 }; setA.UnionWith(setB); Console.WriteLine("After UnionWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } setA = new HashSet<int> { 1, 2, 3, 4 }; setA.IntersectWith(setB); Console.WriteLine("After IntersectWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } } } }
1. What is the main feature of a HashSet?
2. Which method checks if a value exists in a HashSet?
3. Fill in the blanks to add elements to a HashSet<string>.
Danke für Ihr Feedback!