我有一堂课,叫做问题。我还有一个叫做Questions的类,它只是List创建集合的继承。

如何做到这一点,以便可以通过其ID属性而不是像这样的基于整数的索引来检索Question对象?:

Question q = Questions["someidhere"];


基本上,我正在寻找与SqlDataReader对象相同的功能,您可以在其中基于字段名称或索引来检索字段。

最佳答案

完全不要使用List<T>;使用Dictionary<K,V>

var dict = new Dictionary<string, Question>();
dict.Add("someidhere", new Question(...));
// ...
var q = dict["someidhere"];

关于c# - 继承List <T>,希望通过字符串ID值而不是索引来检索项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10232963/

10-13 08:01