正在加载...

HashSet

用 new HashSet<string>() 创建名为 tags 的 HashSet<string>。先 Add "oak",再 "pine",再 "oak"。写入 tags.Count。在 Pine 的 Main 中完成。在 using System; 下保留 using System.Collections.Generic;。用 dotnet run 运行。屏幕应显示一行:2。集合只保留唯一值。同一个词的第二次 Add 不会让 Count 变大。本页较早的检查会把 tags.Contains("pine") 打印为 True。公开起始类名保持 Pine。

唯一值,不是键值对

在 dictionary 页你把成对数据放在 bin 的键下。HashSet 只存值。没有与每个条目配对的第二个 int 类型。一个词只加一次。再加一次不改变大小。把集合想成标签堆:同一标签掉两次仍只有一个标签。tags 是本页你要的集合。
打开 Pine 文件。起始名仍是 Pine。删除仍打印 bin["oak"] 和 bin.Count 的 Dictionary<string, int> bin 残留。删除带 Remove 的 List 残留。删除每一行 ReadLine。干净的 Main 让单行 Count 屏幕可信。
顶部保留 using System; 和 using System.Collections.Generic;。在 Main 里写 HashSet<string> tags = new HashSet<string>();。然后 tags.Add("oak");、tags.Add("pine");、再 tags.Add("oak");。写 Console.WriteLine(tags.Count);。运行前保存。重复 Add 后的这个 Count 是保留演示。另有一个短文件先检查 Contains;保留答案仍是 2。
HashSet 需要一个类型参数:值的种类。这里是 <string>。教学文件里不要写没有尖括号的裸 HashSet tags。

Count after duplicate Add

csharp
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 在 Count 演示前先问 Contains

在 Main 里两次不同的 Add 之后,可以问某个值是否在集合里。写 tags.Add("oak"); 然后 tags.Add("pine");,再写 tags.Contains("pine");。这个短文件是检查点。测验答案仍是重复 Add 后打印 2 的 Count 文件。

Contains after two Adds

csharp
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"));
    }
}

从上往下读保留的 Count 文件。using System; 准备控制台名称。using System.Collections.Generic; 准备 HashSet。public class Pine 是起始类。public static void Main() 是运行开始处。new HashSet<string>() 建空集合。第一个 Add 放入 "oak"。第二个 "pine"。第三个 Add("oak") 发现 oak 已在,所以 Count 仍是 2。写 tags.Count 打印 2。
用 dotnet run 运行。程序写一行就停。如果等你输入,文件里还有 ReadLine — 删掉。屏幕显示 2。若显示 3,可能用了 List 而非 HashSet — 回到 HashSet<string>。若先 3 后 2,可能 dictionary 演示还在跑。若编译找不到 HashSet,恢复 using System.Collections.Generic;。
若写 tags[0] 当列表槽位,编译失败 — 集合没有像 List 那样的索引器。只用 Add、Contains、Count。在任何 Add 之前调用 Contains("oak"),屏幕显示 False。

Add、Contains、Count 并列

Add 在新值时放入。Contains 用 True 或 False 回答是否存在。Count 报告集合里现在有多少不同值。三者都在 new HashSet<string>() 得到的同一个 HashSet<string> tags 上工作。
仍打印 bin["oak"] 和 bin.Count 的残留属于 dictionary 页。本页保留 HashSet<string> tags 和唯一 Add 习惯。把 dictionary 混进 Main 可能打印键控 int 而不是集合 Count。

📊 Add Contains Count

调用 本页回答什么
tags.Add("oak") 若尚未存在则存入 oak
tags.Contains("pine") pine 是否在集合中 (True / False)
tags.Count 集合现在有多少不同值

留给 iterating 的内容

本页建集合、加唯一值、问 Contains、读 Count。下一页 — iterating — 用循环逐个遍历集合值。本页停留在创建、Add、Contains、Count。不要一次发明所有集合方法;课程目标是唯一的 tags 袋。当 dotnet run 从 Count 文件打印 2,你就有了本页检查点。
不要嵌套第二个 Main。文件里不要留 Dictionary 索引演示。若只看到 True,你在 Contains 文件 — 要 2 请用 Count 文件。重复 oak 的三次 Add 后若 Count 是 3,你不在 HashSet 上 — 检查类型和 using。集合上保留 <string>。

你可以写 HashSet<string> tags = new HashSet<string>();,调用 tags.Add("oak");、tags.Add("pine");、再 tags.Add("oak");,然后写 tags.Count。用 dotnet run 运行。屏幕显示 2。重复没有增大集合。较早检查里 Contains("pine") 是 True。把 HashSet 想成唯一标签堆。需要键找配对值时用 Dictionary;只关心出现一次时用集合。Pine 仍是你用 dotnet run 打开的门。下一页是 iterating。
保留 using System.Collections.Generic;。集合类型保留 <string>。把 dictionary 演示移出 Main,以免屏幕误显 3 再 2。这些习惯是 iterating 前的检查点。

🧠 测试您的知识

准备开始

测试您的知识

通过这个互动测验挑战自己,看看你对这个主题的理解程度如何

❓
6
问题
🎯
70%
及格要求
♾️
∞
时间
🔄
∞
尝试次数

📝 说明

  • 仔细阅读每个问题
  • 为每个问题选择最佳答案
  • 您可以随时重新参加测验
  • 您的进度将显示在顶部