自定义按照index和key访问的List
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"];
}
}
以上方法中添加了一个按照名称的索引器,这样访问起来就方便了!
自定义按照index和key访问的List的更多相关文章
- MySQL Block Nested Loop and Batched Key Access Joins(块嵌套循环和批量Key访问连接)
Block Nested-Loop and Batched Key Access Joins Batched Key Access (BKA) Join算法通过index和join buffer访问j ...
- (转)PowerDesigner提示Existence of index、key、reference错误
建立一个表后,为何检测出现Existence of index的警告 A table should contain at least one column, one index, one key ...
- PowerDesigner 提示 Existence of index、key、reference错误
一.建立一个表后,为何检测出现Existence of index的警告 A table should contain at least one column, one index, one key, ...
- React 等框架使用 index 做 key 的问题
React 等框架使用 index 做 key 的问题 假如有两个树,一个是之前,一个是更变之后,我们抽象成两种可能性. 插入内容在最后 插入内容在最前 关于插在中间,原理一样,就不阐述. 使用 ul ...
- index.jsp首页访问不了的解决方法
解决方法: Tomcat,将项目添加到里面 部署解包的webapp目录 将Web项目部署到Tomcat中的方法之一,是部署没有封装到WAR文件中的Web项目.要使用这一方法部署未打包的webapp目录 ...
- 【tomcat】FileNotFoundException: C:\Program Files\Java\apache-tomcat-8.5.11-geneshop3\webapps\ROOT\index.html (拒绝访问。)
新装系统后,tomcat启动起来 提示如下错误: Caused by: java.io.FileNotFoundException: C:\Program Files\Java\apache-tomc ...
- INDEX && PRIMARY KEY && UNIQUE KEY
When I have do some sql tody, some confusion come up to me. Its about the index && PRIMARY K ...
- MySQL中index和key的关系
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY ...
- v-for为什么要加key,能用index作为key么
前言 在vue中使用v-for时,一直有几个疑问: v-for为什么要加key 为什么有时候用index作为key会出错 带着这个疑问,结合各种博客和源码,终于有了点眉目. virtual dom 要 ...
随机推荐
- 多线程中的lua同步问题
最近写paintsnow::start时出现了一个非常麻烦的BUG,程序的Release版本大约每运行十几次就会有一次启动时崩溃(Debug版本还没崩溃过),崩溃点也不固定.经过简单分析之后,确定是线 ...
- UVA 12125 March of the Penguins
题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...
- cover letter issues
All cover letters should: Explain why you are sending a resume. Don't send a resume without a cover ...
- 单击事件的处理方式及注册窗体的创建之(四)Intent实现界面跳转传值
跳转开发步骤: 创建Intent对象 设置Intent对象的跳转路径 启动Intent //Register_Activity.java case R.id.btnRegister: Inte ...
- android中页面的返回刷新
android中从A activity 打开B activity 操作之后返回A activity,并且A activity状态改变就要用到刷新 我就介绍一下我开发中最常用的方法 引用函数 setRe ...
- iOS开发面试题整理 (三)
1. 风格纠错题 修改完的代码: typedef NS_ENUM(NSInteger, CYLSex) { CYLSexMan, CYLSexWoman }; @interface CYLUser : ...
- mysql+php+pdo批量添加大数据
1.使用insert into插入 ini_set('max_execution_time','0');//限制超时时间,因为第一种时间较长,索性设为0不限制 $pdo = new PDO(" ...
- YouTube视频插入Markdown
举个例之: 正常YouTube会生成一个<iframe>直接在HTML里面引用即可: <iframe width="420" height="315&q ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- Ring3 和Ring0 解释
这得从CPU指令系统(用于控制CPU完成各种功能的命令)的特权级别说起.在CPU的所有指令中,有一些指令是非常危险的,如果错用,将导致整个系统崩溃.比如:清内存.设置时钟等.如果所有的程序都能使用这些 ...