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. 金蝶CLOUD消息队列服务

  2. idea整合mybatis逆向工程

    --pom.xml添加插件 <build> <finalName>hnapi</finalName> <plugins> <plugin> ...

  3. D. Fish eating fruit

    题:https://nanti.jisuanke.com/t/41403 题意:求任意俩点之间距离之和模3后的三个结果的总数(原距离之和) 第一种做法: 树形dp #include<bits/s ...

  4. day19-3个双下item方法

    #使用双下item方法来实现属性的增删改查: # 查:__getitem__ 增改:__setitem__ 删除: __delitem__ class Goods: def __init__(self ...

  5. Spring-security整理

    出于某些原因,需要学习一下spring的安全框架.(研究半天,如果单单说用户认证和授权这块儿,我感觉还是shiro好用.) spring security介绍可以参考一下以下文档: (满满的羡慕啊)我 ...

  6. peewee 对 mysql 类型支持问题,并不支持bit

    这个问题是当时想当然了 看到python BooleanField(default=False) 便认为 对应 mysql的bit 出现问题 mysql实际字段存储值为0,但 peewee orm 后 ...

  7. mycat(读写分离、负载均衡、主从切换)

    博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 1.环境准备 1.1新增两台虚拟机 mycat01:192.168.247.81 mycat02:192.168.247 ...

  8. uname|mv|tar -xzvf|

    $ ls CAFE-4.2.1.tar.gz mcl-latest.tar.gz mysql-5.4.3-beta-linux-i686-glibc23.tar.gz.1 orthomclSoftwa ...

  9. python面向对象小tips

    (一).python鸭子类型 python作为动态语言继承和多态与静态语言(像java)有很大的不同:比如说在java中的某个方法,如果传入的参数是Animal类型,那么传入的对象必须是Animal类 ...

  10. RSA key lengths

    RSA key lengths From http://www.javamex.com/tutorials/cryptography/rsa_key_length.shtml When you cre ...