class Program
{
static void Main(string[] args)
{
IndexClass names = new IndexClass();
names[] = "Zara";
names[] = "Riz";
names[] = "Nuha";
names[] = "Asif";
names[] = "Davinder";
names[] = "Sunil";
names[] = "Rubic";
// 使用带有 int 参数的第一个索引器
for (int i = ; i < IndexClass.size; i++)
{
Console.WriteLine(names[i]);
}
// 使用带有 string 参数的第二个索引器
Console.WriteLine(names["Nuha"]);
Console.ReadKey(); }
}
class IndexClass
{
private string[] namelist = new string[size];
public static int size = ;
//// --构造函数赋值
public IndexClass()
{
for (int i = ; i < size; i++)
{
namelist[i] = "N. A.";
} }
//重载索引
public string this[int index]
{
get
{
string tmp; if (index >= && index <= size - )
{
tmp = namelist[index];
}
else
{
tmp = "";
} return (tmp);
}
set
{
if (index >= && index <= size - )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = ;
while (index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
} } }

索引的重载 str["name"] str[i]的更多相关文章

  1. 算法:Manacher,给定一个字符串str,返回str中最长回文子串的长度。

    [题目] 给定一个字符串str,返回str中最长回文子串的长度 [举例] str="123", 1 str="abc1234321ab" 7 [暴力破解] 从左 ...

  2. 为什么字符串String是不可变字符串&&"".equals(str)与str.equals("")的区别

    为什么字符串String是不可变字符串 实际上String类的实现是char类型的数组 虽然说源码中设置的是private final char[] value; final关键词表示不可变动 但是只 ...

  3. 003_python的str切片,str常用操作方法,for循环,集合,深浅copy

    基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余  + - * / ** %... 2.bool布尔值 判断真假以及作为条件变量 3.s ...

  4. "".equals(str)和str.equals("")的区别

    如果当str为null的话 "".equals(str)不会报空指针异常,而str.equals("")会报异常.这种方式主要用来防止空指针异常

  5. Python: 字符串开头或结尾匹配str.startswith(),str.endswith()

    问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...

  6. PyCharm启动报错 TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’ 解决

    这个提示大概是说:"类型错误:不支持操作类型为字符串和字符串",直接把两个字符串(BASE_DIR = os.path.dirname(os.path.dirname(os.pat ...

  7. TypeError: unsupported operand type(s) for |: 'str' and 'str'

    问题描述:

  8. python unicode to str and str to unicode

    @staticmethod def unicode2str(p_unicode): v = p_unicode.encode('unicode-escape').decode('string_esca ...

  9. string str将str转字符数组以及字符数组初始化

    c和指针(<pointers on c>) 8.1.11 一个字符串,如"hello",一般为字符串常量,可以用它对字符指针赋值,或初始化,相当于把这个字符串常量的首地 ...

随机推荐

  1. JVM内存监控工具 Jconsole

    -------------Jconsole监视远程的linux服务器上的tomcat ----------------------------- 1.linux服务器上的tomcat 的bin/cat ...

  2. 专业版Unity技巧分享:使用定制资源配置文件

    http://unity3d.9tech.cn/news/2014/0116/39639.html 通常,在游戏的开发过程中,最终会建立起一些组件,通过某种形式的配置文件接收一些数据.这些可能是程序级 ...

  3. 关于NGUI与原生2D混用相互遮盖的问题心得

    http://www.fzgh.org.cn/zuixindianying/144224.html Native2D自己可以使用Sort Layer来排序,每层又有不同的Order In Layer, ...

  4. HttpClient session

    session概述 session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session ...

  5. linux中comm命令用法

    linux系统中comm命令用法详解 linux系统下的comm命令是一个非常实用的文件对比命令. comm命令功能:   选择或拒绝两个已排序的文件的公共的行. comm命令语法:comm [-12 ...

  6. PYTHON 文件操作

    对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Beautiful is better than ugly. 优美胜于丑陋 Explicit i ...

  7. PHPExcel类的使用讲解

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...

  8. BZOJ 2654: tree

    Description \(n\) 个点, \(m\) 条边,边有权值和黑/白色,求含有 \(need\) 个白边的生成树. Sol 二分+Kruskal. 将每条白边都加上一个权值,然后跑最小生成树 ...

  9. IntelliJ IDEA 导入web项目的一些配置

  10. ModelState.IsValid

    model内的设置如下所示: /// <summary> /// 取得或设置邮编 /// </summary> [RegularExpression(@"(^[1-9 ...