1. c# 索引器(indexer)

using System;
using System.IO; namespace IO
{
class Program
{
private string[] nameList = new string[];
static void Main(string[] args)
{ var names = new IndexedNames();
names[] = "hi";
Console.WriteLine(names[]);
Console.WriteLine(names[]);
Console.WriteLine(names[]);
} class IndexedNames
{
private string[] nameList = new string[];
public IndexedNames()
{
for (int i = ; i < nameList.Length; i++)
{
nameList[i] = "N/A";
}
} public string this[int index]
{
get
{
string tmp;
if (index >= && index < nameList.Length)
{
tmp = nameList[index];
}
else
{
tmp = "index is empty";
}
return tmp;
} set
{
if(index >= && index < nameList.Length)
{
nameList[index] = value;
}
} }
} }
}
using System;
using System.IO; namespace IO
{
class Program
{
private string[] nameList = new string[];
static void Main(string[] args)
{ var names = new IndexedNames();
names[] = "hi";
Console.WriteLine(names[]);
Console.WriteLine(names[]);
Console.WriteLine(names[]); var testStr = "hi";
Console.WriteLine(names[testStr]); } class IndexedNames
{
private string[] nameList = new string[];
public IndexedNames()
{
for (int i = ; i < nameList.Length; i++)
{
nameList[i] = "N/A";
}
} public string this[int index]
{
get
{
string tmp;
if (index >= && index < nameList.Length)
{
tmp = nameList[index];
}
else
{
tmp = "index is empty";
}
return tmp;
} set
{
if(index >= && index < nameList.Length)
{
nameList[index] = value;
}
} } // 以字符串为索引,返回该字符串在数组中的整型索引,重载了索引器
public int this[string name]
{
get
{
int i = ;
while(i < nameList.Length)
{
if(nameList[i] == name)
{
return i;
}
}
return -;
}
}
} }
}

可为get、set 设置修饰符,一般set为private,get为public

3. 基于接口的索引器与代码强壮性

在接口内也可以新建索引器。

下方

using System;
using System.IO; namespace IO
{
class Program
{
private string[] nameList = new string[];
static void Main(string[] args)
{ var names = new IndexedNames();
names[] = "hi";
Console.WriteLine(names[]);
Console.WriteLine(names[]);
Console.WriteLine(names[]); var testStr = "hi";
Console.WriteLine(names[testStr]); }
}
class IndexedNames
{
private string[] nameList = new string[];
public IndexedNames()
{
for (int i = ; i < nameList.Length; i++)
{
nameList[i] = "N/A";
}
} public string this[int index]
{
get
{
string tmp;
if (index >= && index < nameList.Length)
{
tmp = nameList[index];
}
else
{
tmp = "index is empty";
}
return tmp;
} set
{
if (index >= && index < nameList.Length)
{
nameList[index] = value;
}
} } // 以字符串为索引,返回该字符串在数组中的整型索引
public int this[string name]
{
get
{
int i = ;
while (i < nameList.Length)
{
if (nameList[i] == name)
{
return i;
}
}
return -;
}
}
}
public interface ISomeInterface
{
int this[int index]
{
get;
set;
}
}
class IndexerClass : ISomeInterface
{
private int[] arr = new int[];
public int this[int index]
{
get
{
return arr[index];
}
set
{
arr[index] = value;
}
}
}
}

------------恢复内容结束------------

MVC08的更多相关文章

  1. MVC-08模型

    部分7:添加数据模型. MVC模型 MVC模型包含所有应用程序逻辑(业务逻辑.验证逻辑.数据访问逻辑),除了纯视图和控制器逻辑. 通过MVC,模型可保存并操作应用程序数据. Models文件夹 Mod ...

  2. JavaScript学习总结(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  3. Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)

    很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...

  4. 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库

    一.axios Vue更新到2.0之后宣告不再对vue-resource更新,推荐使用axios,axios是一个用于客户端与服务器通信的组件,axios 是一个基于Promise 用于浏览器和 no ...

  5. JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

随机推荐

  1. 吴裕雄--天生自然python机器学习:使用决策树预测隐形眼镜类型

    解决策树如何预测患者需要佩戴的隐形眼镜类型.使用小数据 集,我们就可以利用决策树学到很多知识:眼科医生是如何判断患者需要佩戴的镜片类型:一旦 理解了决策树的工作原理,我们甚至也可以帮助人们判断需要佩戴 ...

  2. samtools faidx

    $ samtools faidx t1.fa && echo "faidx built" $ cat t1.fa.fai scaffold332 scaffold3 ...

  3. html为什么用雪碧图的优缺点

    CSS Sprite(雪碧图/精灵图) 1          概念解释 将小图标和背景图像合并到一张图片上,然后利用css的背景/定位来显示需要显示的图片部分. 2           优点 ① 减少 ...

  4. hdu1069 Monkey and Banana LIS

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...

  5. TreeviewEditor.rar

    本工具可以打开.保存指定格式的XML文件. 树形控件的节点可以编辑.删除.增加.使用本工具看方便地创建书或论文的目录大纲,我用这个工具已经写了好几本书了. 动态图1: 动态图2:编辑效果,支持节点拖曳 ...

  6. 在SpringBoot中使用Junit测试

    一:加入依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ...

  7. 奇点云数据中台技术汇(一) | DataSimba——企业级一站式大数据智能服务平台

    在这个“数据即资产”的时代,大数据技术和体量都有了前所未有的进步,若企业能有效使用数据,让数据赚钱,这必将成为企业数字化转型升级的有力武器. 奇点云自研的一站式大数据智能服务平台——DataSimba ...

  8. Hadoop_在linux中安装hadopp出现的问题

    问题 sudo: no valid sudoers sources found, quitting 网络解决方法 链接:sudo: no valid sudoers sources found, qu ...

  9. GIL锁和进程/线程池

    GIL锁 1.GIL锁 全局解释器锁,就是一个把互斥锁,将并发变成串行,同一时刻只能有一个线程使用共享资源,牺牲效率,保证数据安全,也让程序员避免自己一个个加锁,减轻开发负担 带来的问题 感觉单核处理 ...

  10. Ribosome profiling|N-terminomics|蛋白质基因组学

    生物医学大数据-蛋白质基因组学:质谱注释 蛋白质组与其他组学的关系便是互为印证:蛋白质基因组学原本用于基因组注释,后面扩展到蛋白质与转录组或可变剪接之间关系,同时,蛋白质组依赖于基因组注释作为验证.许 ...