C# HashSet
Create a HashSet<string> named tags with new HashSet<string>(). Add "oak", then add "pine", then add "oak" again. Write tags.Count. Keep the work in Pine with Main. Keep using System.Collections.Generic; under using System;. Run with dotnet run. The screen should show one line: 2. A set keeps unique values. A second Add of the same word does not grow Count. An earlier check on this page prints tags.Contains("pine") as True. Keep the starting public class named Pine.
Unique values, not keyed pairs
On the dictionary page you stored pairs under keys in bin. A HashSet stores values alone. There is no second type for a paired int. You add a word once. Adding it again leaves the set the same size. Think of the set as a pile of tags: dropping the same tag twice still leaves one tag in the pile. The name tags is the set you keep on this page. The values are short words so the screen stays easy to read.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any leftover Dictionary<string, int> bin demo that prints bin["oak"] and bin.Count. Delete any List Remove leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the one-line Count screen easy to trust. Mixing old demos in one Main can print a keyed int instead of the set Count.
At the top keep using System;. Keep using System.Collections.Generic;. Inside Main, write HashSet<string> tags = new HashSet<string>();. Then tags.Add("oak");, tags.Add("pine");, and tags.Add("oak"); again. Write Console.WriteLine(tags.Count);. Save before you run. That Count after the duplicate Add is the kept demo. A separate short file on this page checks Contains first. The Contains run is only a checkpoint; the page's kept answer is still 2.
HashSet needs one type argument: the value kind. On this page that is <string>. Do not write a plain HashSet tags without the type argument in the teaching file. The angle brackets are part of the type name you type in the editor.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
HashSet<string> tags = new HashSet<string>();
tags.Add("oak");
tags.Add("pine");
tags.Add("oak");
Console.WriteLine(tags.Count);
}
}
Pine asks Contains before the Count demo
Inside Main, after two different Adds, you can ask whether a value is present. Write tags.Add("oak"); then tags.Add("pine");, then write tags.Contains("pine"). That short file is a checkpoint. The kept answer for the quiz is still the Count file that prints 2 after a duplicate Add.
using System;
using System.Collections.Generic;
public class Pine
{
public static void Main()
{
HashSet<string> tags = new HashSet<string>();
tags.Add("oak");
tags.Add("pine");
Console.WriteLine(tags.Contains("pine"));
}
}
Read the kept Count file from the top. using System; prepares console names. using System.Collections.Generic; prepares HashSet. public class Pine is the starter. public static void Main() is where the runtime starts. new HashSet<string>() builds an empty set. The first Add stores "oak". The second stores "pine". The third Add("oak") finds oak already present, so Count stays 2. Writing tags.Count prints 2.
Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the set work. The screen shows 2. If the screen shows 3, you may have used a List instead of a HashSet — switch back to HashSet<string>. If the screen shows 3 then 2, a leftover dictionary demo may still be running. If the build cannot find HashSet, restore using System.Collections.Generic;.
If you write tags[0] hoping for a list slot, the build fails — a set has no indexer like List on this page. Stay with Add, Contains, and Count. If you call Contains("oak") before any Add, the screen shows False. Add the values first on the Contains checkpoint.
Add, Contains, and Count side by side
Add puts a value in when it is new. Contains answers presence with True or False. Count reports how many distinct values sit in the set now. All three work on the same HashSet<string> tags you create with new HashSet<string>().
A leftover that prints bin["oak"] and bin.Count is the dictionary page. For this page keep HashSet<string> tags and the unique-Add habit. Mixing a dictionary leftover into Main can print a keyed int instead of the set Count — keep dictionary work out while you practice HashSet.
📊 Add Contains Count
| Call | What it answers on this page |
|---|---|
tags.Add("oak") |
stores oak if it is not already present |
tags.Contains("pine") |
whether pine is present (True / False) |
tags.Count |
how many distinct values the set holds now |
What this leaves for iterating
This page builds a set, adds unique values, asks Contains, and reads Count. A later page — iterating — walks collection values one by one with a loop. Stay with create, Add, Contains, and Count here. Do not invent every set method on this page; the lesson goal is the unique bag of tags itself. When dotnet run prints 2 from the Count file, you have the checkpoint this page asks for. That single number is enough proof that the duplicate Add did not grow the set.
Do not nest a second Main. Do not leave Dictionary indexer demos in the file. If the screen shows only True, you are on the Contains file — use the Count file when you want 2. If Count is 3 after three Adds including a duplicate, you are not on a HashSet — check the type name and the using line. Keep <string> on the set type. If the build cannot find HashSet, put using System.Collections.Generic; back under using System;.
You can write HashSet<string> tags = new HashSet<string>();, call tags.Add("oak");, tags.Add("pine");, and tags.Add("oak"); again, then write tags.Count. Run with dotnet run. The screen shows 2. The duplicate did not grow the set. Contains("pine") was True on the earlier check. Think of HashSet as a pile of unique tags: dropping the same tag twice still leaves one. You still use a dictionary when you need a key that finds a paired value. You use a set when you only care that a value is present once. Pine stays the door you open with dotnet run. The next page is iterating, where you walk values in collections one by one.
Keep using System.Collections.Generic;. Keep <string> on the set type. Keep leftover dictionary demos out of Main so the screen cannot show 3 then 2 by mistake. Those habits are the checkpoint before you move on.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top