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. Matlab高级教程_第三篇:Matlab转码C/C++方式(混编)_第一部分

    0. 其实Matlab的转码混编大多数就是为了现成的算法函数不用再写了,2就是为了方便提高代码运行速度用C语言去运行. 1. MEX文件: Mex文件是一种可在matlab环境中嗲用C语言(或fort ...

  2. mediawiki资料

    1.如何通过ip访问mediawiki --- http://blog.sina.com.cn/s/blog_3f2a2b8e01000awx.html 发布到外部网络,更改htfp.config里面 ...

  3. [USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)

    https://www.luogu.org/problem/P2966 题目描述 Like everyone else, FJ is always thinking up ways to increa ...

  4. 40)PHP,mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别

    分析: mysql_fetch_row,这个函数是从结果集中取一行作为枚举数据,从和指定的结果标识关联的结果集中取得一行数据并作为数组返回.每个结果的列储存在一个数组的单元中,偏移量从 开始. 注意, ...

  5. 吴裕雄--天生自然 JAVA开发学习:网络编程

    import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...

  6. ubantu中的mysql命令

    查看mysql的安装目录:which mysql 进入mysql的运行状态:mysql -uroot -p 56..a_

  7. 常用JS图片滚动(无缝、平滑、上下左右滚动)

    常用JS图片滚动(无缝.平滑.上下左右滚动)代码大全 <head><-----></head><body> <!--向下滚动代码开始-->& ...

  8. Uber退出东南亚留下烂摊子,给“中国式并购”带来哪些启示

    当下,从全球范围内来看很多互联网企业都采用了"复制+粘贴"的疯狂推进模式.它们往往在某一个国家或地区取得领先优势后,就快速将相同模式在全球推进去占领当地市场.无论结果是一家独大占据 ...

  9. 82)PHP,基本框架类步骤

    framework.class.php 基本代码展示: <?php /** * 框架初始化功能类 */ class Framework { /** * 入口 * 里面的static和self是一 ...

  10. centos 中文乱码解决办法2

    特别注意:vm10自带虚拟机共享工具,自动识别utf-8格式的中文文件内容 下面步骤摘自网络: 两种方案其实差不多,这里提供两个文件的下载地址,免得去网上搜这两个文件了: fonts-chinese- ...