【问题标题】:How to set a HashSet<string> as a Value in Dictionary?如何将 HashSet<string> 设置为字典中的值?
【发布时间】:2022-01-18 21:09:17
【问题描述】:

我有一个称为“主题”的字符串数组。 我正在尝试填充我的字典并使用某个类作为键和 HashSet“字符串”作为值,但无法正确完成并收到错误“无法将字符串 [] 转换为 System.Collections.Generic.HashSet”字符串“ ”。 那么我需要在我的字典中添加什么才能使其正常工作?

Dictionary<Student, HashSet<string>> studentSubjectDict = new Dictionary<Student, HashSet<string>>();

studentSubjectDict.Add(Student11, subjects);

【问题讨论】:

  • new HashSet&lt;string&gt;(subjects)subjects.ToHashSet()(使用 LINQ 和最新版本的 .NET)。
  • 让你的学生有一个属性 Subjects 不是更明智(如果你愿意,它可以是一个哈希集)
  • 感谢@Jeroen Mostert 解决了问题

标签: c# dictionary hashset


【解决方案1】:

正如@Jeron Mostert 所说,new HashSet&lt;string&gt;(subjects)subjects.ToHashSet() 解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2020-10-15
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多