HashSet<T>, SortedSet<T>, Queue<T>, Stack<T>
Understand unique sets vs ordered sets and FIFO/LIFO collections; practice Add/Contains, Enqueue/Dequeue, Push/Pop.
HashSet<T>, SortedSet<T>, Queue<T>, Stack<T> is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Shapes of collections
Goal: Pick the right collection.
- HashSet<T>: unique, fast lookup
- SortedSet<T>: unique + sorted
- Queue<T>: FIFO
- Stack<T>: LIFO
HashSet basics
HashSet<T> keeps unique values; Add returns false on duplicates; Contains is fast.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
HashSet<string> tags = new HashSet<string>();
bool a1 = tags.Add("red"); // true
bool a2 = tags.Add("blue"); // true
bool a3 = tags.Add("red"); // false (duplicate ignored)
Console.WriteLine("Has red? " + tags.Contains("red"));
Console.WriteLine("Count = " + tags.Count); // 2
}
}
SortedSet basics
SortedSet<T> keeps items unique and sorted as you add them.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
SortedSet<int> scores = new SortedSet<int>();
scores.Add(30);
scores.Add(10);
scores.Add(20);
scores.Add(20); // duplicate ignored
foreach (int s in scores)
{
Console.WriteLine(s); // 10, 20, 30 (sorted ascending)
}
Console.WriteLine("Min = " + (scores.Count > 0 ? scores.Min : 0));
Console.WriteLine("Max = " + (scores.Count > 0 ? scores.Max : 0));
}
}
Queue (FIFO)
Queue<T> is FIFO: Enqueue → Dequeue. Use Peek to look without removing.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
Queue<string> q = new Queue<string>();
q.Enqueue("A");
q.Enqueue("B");
q.Enqueue("C");
Console.WriteLine("Peek = " + q.Peek()); // A
Console.WriteLine(q.Dequeue()); // A
Console.WriteLine(q.Dequeue()); // B
Console.WriteLine("Count = " + q.Count); // 1
}
}
Stack (LIFO)
Stack<T> is LIFO: Push → Pop. Peek inspects the top item.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
Stack<int> st = new Stack<int>();
st.Push(1);
st.Push(2);
st.Push(3);
Console.WriteLine("Peek = " + st.Peek()); // 3
Console.WriteLine(st.Pop()); // 3
Console.WriteLine(st.Pop()); // 2
Console.WriteLine("Count = " + st.Count); // 1
}
}
Selection tips
Cheat sheet:
- Need uniqueness + fast lookup → HashSet<T>
- Need uniqueness + sorted order → SortedSet<T>
- Process in arrival order → Queue<T> (FIFO)
- Process last come first → Stack<T> (LIFO)
Set choice
Recap
Recap: Use sets for uniqueness (HashSet, SortedSet) and choose FIFO (Queue) or LIFO (Stack) for ordered processing.
Frequently asked questions
Is the “HashSet<T>, SortedSet<T>, Queue<T>, Stack<T>” lesson free?
Yes — the full text of “HashSet<T>, SortedSet<T>, Queue<T>, Stack<T>” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “HashSet<T>, SortedSet<T>, Queue<T>, Stack<T>”?
Understand unique sets vs ordered sets and FIFO/LIFO collections; practice Add/Contains, Enqueue/Dequeue, Push/Pop. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “HashSet<T>, SortedSet<T>, Queue<T>, Stack<T>” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- HashSet , SortedSet , Queue , Stack
- ConcurrentDictionary , immutable collections
- Equality & hashing (value vs reference)