我有一个名为问题的类。我有另一个名为 Questions 的类,它只是继承 List 创建一个集合。
我怎样才能通过它的 ID 属性而不是像这样基于整数的索引来检索问题对象?:
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/