List<T>用起来比较方便,但是有时候要按照Index来访问List中的对象有些繁琐,所以想是不是扩展一下,既能按照Index来访问,又能按照Key访问。

  实现方法:

    public class Person
{
public string Name { get; set; }
public int Age { get; set; }
} public class PersonCollecton : List<Person>
{
public Person this[string name]
{
get
{
for (int i = ; i < this.Count; i++)
{
if (this[i].Name == name) return this[i];
}
return null;
}
}
} public calss Test
{
static void Main()
{
PersonCollection persons = new PersonCollection();
persons.Add(new Person(){Name = "Li Lei", Age = };
persons.Add(new Person(){Name = "Han Meimei", Age = }; Person HanMeimei = persons["Han Meimei"];
}
}

  以上方法中添加了一个按照名称的索引器,这样访问起来就方便了!

最新文章

  1. 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用
  2. C# Interface的使用方法探讨
  3. linux下的device tree
  4. jquery插件之表格隔行变色并鼠标滑过高亮显示
  5. linux服务器默认连接数配置
  6. 解决错误: java.lang.NoClassDefFoundError: antlr/RecognitionException
  7. Spring EL ternary operator (if-then-else) example
  8. 【Beta】Phylab2.0: Postmortem
  9. Struts2标签库之iterator
  10. Debian 7.1设置中文环境
  11. 蓝桥网试题 java 入门训练 序列求和
  12. 1754: [Usaco2005 qua]Bull Math
  13. javascript中类式继承和原型式继承的实现方法和区别
  14. testng 异常 截图
  15. 编程心法 之 Scrum - Agile 敏捷开发
  16. OID的编解码(即在报文中的体现)
  17. liunx必知必会(1)
  18. redis 集群java.lang.NoSuchMethodError:SpringJAR包版本冲突错误解决方法
  19. Jquery-无法有效获取当前窗口高度
  20. jquery作业 教授答案

热门文章

  1. MTK 2G芯片使用联通卡在深圳无法拨打112原因
  2. 最逼近Mac OS的Linux系统 -- Elementary OS
  3. 登录DSCCC控制台报错提示:安装错误代码: 3
  4. NDK下 将Platinum SDK 编译成so库 (android - upnp)
  5. 程序员使用Node的十个技巧
  6. C盘扩容,超详细,史上最简单的扩容技术贴!
  7. dojo.io.script
  8. C#事件、委托简单示例
  9. 如何写一个数据库How do you build a database?(转载)
  10. Foundation--NSDictionary+NSMutableDictionary