MVC08
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的更多相关文章
- MVC-08模型
部分7:添加数据模型. MVC模型 MVC模型包含所有应用程序逻辑(业务逻辑.验证逻辑.数据访问逻辑),除了纯视图和控制器逻辑. 通过MVC,模型可保存并操作应用程序数据. Models文件夹 Mod ...
- JavaScript学习总结(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...
- 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库
一.axios Vue更新到2.0之后宣告不再对vue-resource更新,推荐使用axios,axios是一个用于客户端与服务器通信的组件,axios 是一个基于Promise 用于浏览器和 no ...
- JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
随机推荐
- http跳转https反向代理配置
一.多数项目会有多个域名,把多个域名写在一个conf文件里,比如命名为proxy.conf文件,这里以888.com这个域名为例,在代理机器上配置 server { listen 80; server ...
- JQ和JS的等价代码
JQ与JS等价代码 选择器 //jquery var els = $(".el"); //原生方法 var els = document.querySelectorAll(&q ...
- [LC] 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- 32)PHP,遍历对象的属性或者属性值
首先是遍历属性: <?php class A{ ; ; ; function fetchAllProp(){ //遍历时,key取得属性名,value取得对应值 foreach($this as ...
- 吴裕雄--天生自然python学习笔记:python 用pygame模块角色类(Sprite)移动与碰撞
角色类(Sprite) Py game 游戏中有许多组件会重复用到,比如射击宇宙飞船的游戏中,外星宇宙 飞船可能多达数十艘 , 通过创建“角色类”,可以生成多个相同的对象. Py game 角色类是游 ...
- linux中的文件解压命令
http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...
- php7 安装mongodb扩展
下载 mongodb-1.6.0.tgz wget https://pecl.php.net/get/mongodb-1.6.0.tgz 版本太低的话有些语法不一样,起码1.5以上吧 进入 mo ...
- Lua-文件操作
简单模式和完全模式 简单模式(simple model)拥有一个当前输入文件和一个当前输出文件,并且提供针对这些文件相关的操作. 完全模式(complete model) 使用外部的文件句柄来实现.它 ...
- 吴裕雄--天生自然 R语言开发学习:基础知识
1.基础数据结构 1.1 向量 # 创建向量a a <- c(1,2,3) print(a) 1.2 矩阵 #创建矩阵 mymat <- matrix(c(1:10), nrow=2, n ...
- JVM如何判断对象能否被回收
•写在前面说起Java和C++,很容易想到让人疯狂的指针,Java使用了内存动态分配和垃圾回收技术,让我们从C++的各种指针问题中摆脱出来,更加专心于业务逻辑,不过如果我们需要深入了解java的JVM ...