HashSet<T>、SortedSet<T>、Queue<T>、Stack<T>
理解唯一集合与有序集合的区别,以及 FIFO/LIFO 集合;练习 Add/Contains、Enqueue/Dequeue、Push/Pop。
HashSet<T>、SortedSet<T>、Queue<T>、Stack<T> 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
集合的类型
目标:选择合适的集合。
- HashSet<T>:唯一、查找快速
- SortedSet<T>:唯一且有序
- Queue<T>:FIFO
- Stack<T>:LIFO
HashSet 基础
HashSet<T> 会保留唯一值;重复值会使 Add 返回 false;Contains 的速度很快。
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 基础
SortedSet<T> 会在添加元素时保持元素唯一且有序。
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));
}
}
队列(FIFO)
Queue<T> 遵循 FIFO:Enqueue → Dequeue。请使用 Peek 在不移除元素的情况下查看元素。
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
}
}
栈(LIFO)
Stack<T> 遵循 LIFO:Push → Pop。Peek 会查看栈顶元素。
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
}
}
选择提示
速查表:
- 需要唯一性和快速查找 → HashSet<T>
- 需要唯一性和有序排列 → SortedSet<T>
- 按到达顺序处理 → Queue<T>(FIFO)
- 后到的元素先处理 → Stack<T>(LIFO)
集合选择
总结
总结:使用集合确保唯一性(HashSet、SortedSet),并根据有序处理需求选择 FIFO(Queue)或 LIFO(Stack)。
常见问题解答
「HashSet<T>、SortedSet<T>、Queue<T>、Stack<T>」课时是免费的吗?
是的 — 「HashSet<T>、SortedSet<T>、Queue<T>、Stack<T>」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「HashSet<T>、SortedSet<T>、Queue<T>、Stack<T>」这节课中我会学到什么?
理解唯一集合与有序集合的区别,以及 FIFO/LIFO 集合;练习 Add/Contains、Enqueue/Dequeue、Push/Pop。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。
「HashSet<T>、SortedSet<T>、Queue<T>、Stack<T>」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- HashSet 、SortedSet 、Queue 、Stack
- ConcurrentDictionary 与不可变集合
- 相等性与哈希(值与引用)