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. 吴裕雄--天生自然C语言开发:enum(枚举)

    enum DAY { MON=, TUE, WED, THU, FRI, SAT, SUN }; enum DAY { MON=, TUE, WED, THU, FRI, SAT, SUN }; en ...

  2. 关于Apache Commons-Lang3的使用

    在日常工作中,我们经常要使用到一些开源工具包,比如String,Date等等.有时候我们并不清楚有这些工具类的存在,造成在开发过程中重新实现导致时间浪费,且开发的代码质量不佳.而apache其实已经提 ...

  3. wait操作接口

    1.等待回收的两个函数wait()和waitpid()函数 1.1 wait(int *status)的用法:阻塞函数,等待任意一个子进程的返回.   *wait(NULL):对子进程的结束状态不关心 ...

  4. 2018安卓巴士开发者大会打造Android技术盛宴

    2018安卓巴士开发者大会打造Android技术盛宴2018安卓巴士开发者大会将于8月25日在上海举行,作为中国最具前沿性.专业性的安卓技术会议,将邀请来自爱奇艺.阿里.饿了么等知名企业的一线工程师分 ...

  5. The Pomodoro Technique

    目录 简介 What to solve How to use Some applications 自我总结 结束语 简介 番茄工作法是简单易行的时间管理方法,是由弗朗西斯科·西里洛于1992年创立的一 ...

  6. TPO1-3 Timberline Vegetation on Mountains

    Wind velocity also increase with altitude and may cause serious stress for trees,as is made evident ...

  7. 常用的SQL优化

    转自:https://www.cnblogs.com/Cheney222/articles/5876382.html 一.优化 SQL 语句的一般步骤 1 通过 show status 命令了解各种 ...

  8. 使用mybatis的动态sql解析能力生成sql

    需求: 计算平台,有很多表,打算提供一个基于sql的服务接口, sql不能完全在配置页面写死, 要能根据参数不同执行不同的语义,防止sql个数爆炸 把mybatis原码down下来, 改造一下测试用例 ...

  9. git本地仓库目录问题

    git安装后修改默认的路径:每次打开git bash后都会进入这个目录 https://blog.csdn.net/weixin_39634961/article/details/79881140 在 ...

  10. 10X genomics|cell base|in-vivo based|model organisms|SBI|

    生命组学-药物基因组学 精准医学的内容有个人全基因组测序,移动可穿戴设备,它可以实时监测,深度学习模型预测疾病,对疾病预测做到有效.安全和可控. 药物基因组学就是研究疾病.化合物和靶点之间的关系,关键 ...