C#通用集合帮助
我需要实现某种字典对象。类似于C#通用集合帮助,c#,collections,C#,Collections,我需要实现某种字典对象。类似于MyDict的集合类被定义为System.Collections或System.Collections.Generic命名空间的一部分 大致如下: Dictionary<Question,Answer> qDict = new Dictionary<Question, Answer>(); Answer attempt_v2 = new Answer(); Question question
MyDict的集合类被定义为System.Collections
或System.Collections.Generic
命名空间的一部分
大致如下:
Dictionary<Question,Answer> qDict = new Dictionary<Question, Answer>();
Answer attempt_v2 = new Answer();
Question question = new Question();
if (qDict.ContainsKey(question))
{
Answer actual_answerv1 = qDict[question];
if (actual_answerv1 == attempt_v2)
{
// Answers match
}
}
Dictionary qDict=newdictionary();
答案尝试_v2=新答案();
问题=新问题();
如果(qDict.ContainsKey(问题))
{
回答实际问题v1=qDict[问题];
如果(实际应答V1==尝试应答v2)
{
//答案匹配
}
}
[注意:我刚刚提出了这个问题。它可能不是解决问题和答案匹配问题的最佳解决方案…]如果您使用的是.NET 4,那么您可以使用几个通用类
因此,您可以使用:
Dictionary<T,Tuple<V1,V2>>
字典
在早期版本中,可以使用以下作为第二个泛型类型:
Dictionary<T,KeyValuePair<V1,V2>>
字典
我认为需要一个新的实现
或者,如果您的答案选择了一个属性,则可以创建
Dictionary<Question, IList<Answer>>
哪个版本的.NET Framework?很有趣。未来可能也会有多个正确答案。所以我应该只创建类型而不使用任何泛型。类似于mytype类{string Question;IList CorrectAnswers;IList SelectedAnswers;}}}感谢您的帮助。应该是这样的。
class Answers {
Ilist Answers;
Answer Selected;
}