【发布时间】: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<string>(subjects)或subjects.ToHashSet()(使用 LINQ 和最新版本的 .NET)。 -
让你的学生有一个属性 Subjects 不是更明智(如果你愿意,它可以是一个哈希集)
-
感谢@Jeroen Mostert 解决了问题
标签: c# dictionary hashset