自定义按照index和key访问的List
2024-10-14 20:50:20
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"];
}
}
以上方法中添加了一个按照名称的索引器,这样访问起来就方便了!
最新文章
- 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用
- C# Interface的使用方法探讨
- linux下的device tree
- jquery插件之表格隔行变色并鼠标滑过高亮显示
- linux服务器默认连接数配置
- 解决错误: java.lang.NoClassDefFoundError: antlr/RecognitionException
- Spring EL ternary operator (if-then-else) example
- 【Beta】Phylab2.0: Postmortem
- Struts2标签库之iterator
- Debian 7.1设置中文环境
- 蓝桥网试题 java 入门训练 序列求和
- 1754: [Usaco2005 qua]Bull Math
- javascript中类式继承和原型式继承的实现方法和区别
- testng 异常 截图
- 编程心法 之 Scrum - Agile 敏捷开发
- OID的编解码(即在报文中的体现)
- liunx必知必会(1)
- redis 集群java.lang.NoSuchMethodError:SpringJAR包版本冲突错误解决方法
- Jquery-无法有效获取当前窗口高度
- jquery作业 教授答案
热门文章
- MTK 2G芯片使用联通卡在深圳无法拨打112原因
- 最逼近Mac OS的Linux系统 -- Elementary OS
- 登录DSCCC控制台报错提示:安装错误代码: 3
- NDK下 将Platinum SDK 编译成so库 (android - upnp)
- 程序员使用Node的十个技巧
- C盘扩容,超详细,史上最简单的扩容技术贴!
- dojo.io.script
- C#事件、委托简单示例
- 如何写一个数据库How do you build a database?(转载)
- Foundation--NSDictionary+NSMutableDictionary