索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的。

索引器和数组比较:

(1)索引器的索引值(Index)类型不受限制

(2)索引器允许重载

(3)索引器不是一个变量

索引器和属性的不同点

(1)属性以名称来标识,索引器以函数形式标识

(2)索引器可以被重载,属性不可以

(3)索引器不能声明为static,属性可以

一个简单的索引器例子

using System;
using System.Collections;

public class IndexerClass
{
private string[] name = new string[2]; //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
public string this[int index]
{
//实现索引器的get方法
get
{
if (index < 2)
{
return name[index];
}
return null;
} //实现索引器的set方法
set
{
if (index < 2)
{
name[index] = value;
}
}
}
}
public class Test
{
static void Main()
{
//索引器的使用
IndexerClass Indexer = new IndexerClass();
//“=”号右边对索引器赋值,其实就是调用其set方法
Indexer[0] = "张三";
Indexer[1] = "李四";
//输出索引器的值,其实就是调用其get方法
Console.WriteLine(Indexer[0]);
Console.WriteLine(Indexer[1]);
}
}

 以字符串作为下标,对索引器进行存取

public class IndexerClass
{
//用string作为索引器下标的时候,要用Hashtable
private Hashtable name = new Hashtable(); //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
public string this[string index]
{
get { return name[index].ToString();
set { name.Add(index, value); }
}
}
public class Test
{
static void Main()
{
IndexerClass Indexer = new IndexerClass();
Indexer["A0001"] = "张三";
Indexer["A0002"] = "李四";
Console.WriteLine(Indexer["A0001"]);
Console.WriteLine(Indexer["A0002"]);
}
}

 索引器的重载

public class IndexerClass
{
private Hashtable name = new Hashtable(); //1:通过key存取Values
public string this[int index]
{
get { return name[index].ToString(); }
set { name.Add(index, value); }
} //2:通过Values存取key
public int this[string aName]
{
get
{
//Hashtable中实际存放的是DictionaryEntry(字典)类型,如果要遍历一个Hashtable,就需要使用到DictionaryEntry
foreach(DictionaryEntry d in name)
{
if (d.Value.ToString() == aName)
{
return Convert.ToInt32(d.Key);
}
}
return -1;
}
set
{
name.Add(value, aName);
}
}
}
public class Test
{
static void Main()
{
IndexerClass Indexer = new IndexerClass(); //第一种索引器的使用
Indexer[1] = "张三";//set访问器的使用
Indexer[2] = "李四";
Console.WriteLine("编号为1的名字:" + Indexer[1]);//get访问器的使用
Console.WriteLine("编号为2的名字:" + Indexer[2]); Console.WriteLine();
//第二种索引器的使用
Console.WriteLine("张三的编号是:" + Indexer["张三"]);//get访问器的使用
Console.WriteLine("李四的编号是:" + Indexer["李四"]);
Indexer["王五"] = 3;//set访问器的使用
Console.WriteLine("王五的编号是:" + Indexer["王五"]);
}
}

多参索引器

using System;
using System.Collections; //入职信息类
public class EntrantInfo
{
//姓名、编号、部门
private string name;
private int number;
private string department;
public EntrantInfo()
{ }
public EntrantInfo(string name, int num, string department)
{
this.name = name;
this.number = num;
this.department = department;
} public string Name
{
get { return name; }
set { name = value; }
} public int Num
{
get { return number; }
set { number = value; }
} public string Department
{
get { return department; }
set { department = value; }
}
} //声明一个类EntrantInfo的索引器
public class IndexerForEntrantInfo
{
private ArrayList ArrLst;//用于存放EntrantInfo类
public IndexerForEntrantInfo()
{
ArrLst = new ArrayList();
} //声明一个索引器:以名字和编号查找存取部门信息
public string this[string name, int num]
{
get
{
foreach (EntrantInfo en in ArrLst)
{
if (en.Name == name && en.Num == num)
{
return en.Department;
}
}
return null;
}
set
{
//new关键字:C#规定,实例化一个类或者调用类的构造函数时,必须使用new关键
ArrLst.Add(new EntrantInfo(name, num, value));
}
} //声明一个索引器:以编号查找名字和部门
public ArrayList this[int num]
{
get
{
ArrayList temp = new ArrayList();
foreach (EntrantInfo en in ArrLst)
{
if (en.Num == num)
{
temp.Add(en);
}
}
return temp;
}
} //还可以声明多个版本的索引器...
} public class Test
{
static void Main()
{
IndexerForEntrantInfo Info = new IndexerForEntrantInfo();
//this[string name, int num]的使用
Info["张三", 101] = "人事部";
Info["李四", 102] = "行政部";
Console.WriteLine(Info["张三", 101]);
Console.WriteLine(Info["李四", 102]); Console.WriteLine(); //this[int num]的使用
foreach (EntrantInfo en in Info[102])
{
Console.WriteLine(en.Name);
Console.WriteLine(en.Department);
}
}
}

C#索引器的用法的更多相关文章

  1. Asp.Net中索引器的用法

    索引器定义类似于属性,但其功能与属性并不相同.索引器提供一种特殊的方法编写get和set访问器.属性可以像访问字段一样访问对象的数据,索引器可以使用户像访问数组一样访问类成员. 一.索引器特性 1.g ...

  2. c#索引器的简单用法

    abstract class Bird { protected string name; public abstract string Name { get; set; } public abstra ...

  3. 如何使用T-SQL备份还原数据库及c#如何调用执行? C#中索引器的作用和实现。 jquery控制元素的隐藏和显示的几种方法。 localStorage、sessionStorage用法总结 在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一) span<T>之高性能字符串操作实测

    如何使用T-SQL备份还原数据库及c#如何调用执行? 准备材料:Microsoft SQL Server一部.需要还原的bak文件一只 一.备份 数据库备份语句:user master backup ...

  4. C#中的索引器的简单理解和用法

    索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...

  5. C#索引器的应用:自已写一个表格

    C#中索引器,在一个类中有很多的同一类型成员的时候,比较适用索引器. 环境:我们假设有一个动物园,里边有很多动物. 用法: 1.先定义一个类,这是成员的类型.在这里就是要定义一个Animal类: pu ...

  6. (转)c# 属性与索引器

    属性是一种成员,它提供灵活的机制来读取.写入或计算私有字段的值. 属性可用作公共数据成员,但它们实际上是称为“访问器”的特殊方法. 这使得可以轻松访问数据,还有助于提高方法的安全性和灵活性. 一个简单 ...

  7. C# 中常用的索引器

    使用 C# 中的索引器和 JavaScript 中访问对象的属性是很相似. 之前了解过索引器,当时还把索引器和属性给记混了, 以为索引器就是属性,下面写下索引器和属性的区别,以及怎么使用索引器 先说明 ...

  8. C#索引器-有参属性

    总结 只要类中有类似于属性的元素就应创建索引器,此属性代表的不是一个值,而是值的集合,其中每一个项由一组参数标识. 这些参数可以唯一标识应引用的集合中的项. 索引器延伸了属性的概念,索引器中的一个成员 ...

  9. 【.net 深呼吸】细说CodeDom(7):索引器

    在开始正题之前,先补充一点前面的内容. 在方法中,如果要引用方法参数,前面的示例中,老周使用的是 CodeVariableReferenceExpression 类,它用于引用变量,也适用于引用方法参 ...

随机推荐

  1. android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable

    第一个感叹号 是跟你说 让你把Imageview 和textview 结合起来 只用 textview textview有个属性叫  android:drawable...(top/bottom/.. ...

  2. Linux 下 git的使用

    参考链接:http://www.liaoxuefeng.com 安装 安装步骤: ①先给操作系统装入git工具,以Linux为例: $ sudo apt-get install git ②去githu ...

  3. 性能更好的js动画实现方式---requestAnimationFrame

    用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是 ...

  4. OpenGL学习--------颜色的选择

    OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式.无论哪种颜色模式,计算机都必须为每一个像素保存一些数据.不同的是,RGBA模式中,数据直接就代表了颜色:而颜色索引模式中,数据代表的是 ...

  5. 修改sqlserver的数据库排序规则语句

    alter database SOETMS collate Chinese_PRC_CI_AS

  6. 深入Java单例模式(转)

    深入Java单例模式 源自 http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容 ...

  7. 解决Ubuntu系统中文乱码显示问题,终端打开文件及查看目录

    解决Ubuntu系统中文乱码显示问题 [日期:2014-02-20] 来源:Linux社区  作者:njchenyi [字体:大 中 小]   我是先安装了Ubuntu 12.04 Server,然后 ...

  8. Linux服务器自动备份压缩MySQL数据库的实用方法

    <?php$server = 'localhost'; $link = mysql_connect($server, 'root', 'haven'); $result = mysql_quer ...

  9. 关于Bean\Entity\Model\POJO的一些个人理解

    本文没有长篇累牍的,严格的,标准的表述,只是我在开发过程中,读书过程中的一些个人理解,可能不太准备,但是我觉得应该是最方便初学者理解的吧? 一.Bean 对于Bean而言,我的理解是只要是Java的类 ...

  10. zepto学习之路--数组去重和原生reduce

    好吧开始读zepto的源代码,最前面给处理trim和reduce的原生实现,感觉写的很紧凑,其中reduce写的有点晦涩,个人感觉还不错.主要zepto的作者是无分号党,看起了有点不习惯. 3 if ...