本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用。

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

索引器和数组比较:

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

2)索引器允许重载

3)索引器不是一个变量

索引器和属性的不同点:

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

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

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

一、下面代码演示一个简单的索引器:

    class Program
{
/// <summary>
/// 简单的索引器类
/// </summary>
public class SimpleIndexerClass
{
private readonly string[] name = new string[]; /// <summary>
/// 索引器必须以this关键字定义,其实这个this就是类实例化之后的对象。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this[int index]
{
get //实现索引器的get方法
{
if (index < )
{
return name[index];
}
return null;
}
set //实现索引器的set方法
{
if (index < )
{
name[index] = value;
}
}
}
} static void Main(string[] args)
{
#region 简单的索引器
//索引器的使用
SimpleIndexerClass indexer = new SimpleIndexerClass();
//“=”号右边对索引器赋值,其实就是调用其set方法。
indexer[] = "张三";
indexer[] = "李四";
//输出索引器的值,其实就是调用其get方法。
Console.WriteLine(indexer[]);
Console.WriteLine(indexer[]);
Console.Read();
#endregion
}
}

运行结果如下:

二、下面代码演示以字符串作为下标,对索引器进行存取。

    class Program
{
/// <summary>
/// 以字符串作为下标,对索引器进行存取。
/// </summary>
public class StringIndexerClass
{
//用string作为索引器下标的时候,要用Hashtable。
private Hashtable name = new Hashtable(); /// <summary>
/// 索引器必须以this关键字定义,其实这个this就是类实例化之后的对象。
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this[string index]
{
get
{
return name[index].ToString();
}
set
{
name.Add(index, value);
}
}
} static void Main(string[] args)
{
#region 以字符串作为下标,对索引器进行存取。
StringIndexerClass Indexer = new StringIndexerClass();
Indexer["A0001"] = "张三";
Indexer["A0002"] = "李四";
Console.WriteLine(Indexer["A0001"]);
Console.WriteLine(Indexer["A0002"]);
Console.Read();
#endregion
}
}

运行结果如下:

三、下面代码演示索引器的重载。

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

运行结果如下:

四、多参索引器

    class Program
{
/// <summary>
/// 入职信息类
/// </summary>
public class EntrantInfo
{
//工号
public int JobNumber { get; set; }
//姓名
public string Name { get; set; }
//部门
public string Department { get; set; }
/// <summary>
/// 无参构造函数
/// </summary>
public EntrantInfo()
{ }
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="jobNumber">工号</param>
/// <param name="name">姓名</param>
/// <param name="department">部门</param>
public EntrantInfo(int jobNumber, string name, string department)
{
JobNumber = jobNumber;
Name = name;
Department = department;
}
} //声明一个类EntrantInfo的索引器
public class EntrantInfoIndexerClass
{
private readonly ArrayList arrayList; //用于存放EntrantInfo类
public EntrantInfoIndexerClass()
{
arrayList = new ArrayList();
} //声明一个索引器:以姓名及部门查找工号。
public int this[string name, string department]
{
get
{
foreach (EntrantInfo ei in arrayList)
{
if (ei.Name == name && ei.Department == department)
{
return ei.JobNumber;
}
}
return -;
}
set
{
//new关键字:C#规定,实例化一个类或者调用类的构造函数时,必须使用new关键。
arrayList.Add(new EntrantInfo(value, name, department));
}
} //声明一个索引器:以工号查找姓名和部门。
public ArrayList this[int jobNumber]
{
get
{
ArrayList alFind = new ArrayList();
foreach (EntrantInfo ei in arrayList)
{
if (ei.JobNumber == jobNumber)
{
alFind.Add(ei);
}
}
return alFind;
}
} //还可以声明多个版本的索引器...
} static void Main(string[] args)
{
#region 多参索引器
EntrantInfoIndexerClass indexer = new EntrantInfoIndexerClass();
//this[string name, string department]的使用
indexer["张三", "行政人事部"] = ;
indexer["李四", "行政人事部"] = ;
Console.WriteLine("行政人事部张三的工号:" + indexer["张三", "行政人事部"]);
Console.WriteLine("行政人事部李四的工号:" + indexer["李四", "行政人事部"]);
Console.WriteLine();
//this[int jobNumber]的使用
foreach (EntrantInfo ei in indexer[])
{
Console.WriteLine("工号101的姓名:" + ei.Name);
Console.WriteLine("工号101的部门:" + ei.Department);
}
Console.Read();
#endregion
}
}

运行结果如下:

C#索引器学习笔记的更多相关文章

  1. Android安装器学习笔记(一)

    Android安装器学习笔记(一) 一.Android应用的四种安装方式: 1.通过系统应用PackageInstaller.apk进行安装,安装过程中会让用户确认 2.系统程序安装:在开机的时候自动 ...

  2. MySQL索引知识学习笔记

    目录 一.索引的概念 二.索引分类 三.索引用法 四 .索引架构简介 五.索引适用的情况 六.索引不适用的情况 继我的上篇博客:Oracle索引知识学习笔记,再记录一篇MySQL的索引知识学习笔记,本 ...

  3. C# 索引器 学习

    转载原地址: http://www.cnblogs.com/lxblog/p/3940261.html 1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类 ...

  4. MongoDB索引05-30学习笔记

    MongoDB 索引 索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录. 这种扫描全集合的查询效率是非常低的,特别在处理大 ...

  5. Oracle索引知识学习笔记

    目录 一.Oracle索引简介 1.1 索引分类 1.2 索引数据结构 1.3 索引特性 1.4 索引使用注意要点 1.5.索引的缺点 1.6.索引失效 二.索引分类介绍 2.1.位图索引 1.2.函 ...

  6. 深入浅出 Vue.js 第九章 解析器---学习笔记

    本文结合 Vue 源码进行学习 学习时,根据 github 上 Vue 项目的 package.json 文件,可知版本为 2.6.10 解析器 一.解析器的作用 解析器的作用就是将模版解析成 AST ...

  7. Web 在线文件管理器学习笔记与总结(19)上传文件

    dir.func.php 中添加方法: /* 上传文件 */ function uploadFile($fileInfo,$path,$allowExt = array('jpg','jpeg','p ...

  8. Web 在线文件管理器学习笔记与总结(17)复制文件 (18)剪切文件

    (17)复制文件 ① 复制文件通过copy($src,$dst) 来实现 ② 检测目标目录是否存在,如果存在则继续检测目标目录中是否存在同名文件,如果不存在则复制成功 file.func.php 中添 ...

  9. Web 在线文件管理器学习笔记与总结(15)剪切文件夹 (16)删除文件夹

    (15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 ...

随机推荐

  1. Oracle 重启监听

    对于DBA来说,启动和关闭oracle监听器是很基础的任务,但是Linux系统管理员或者程序员有时也需要在开发数据库中做一些基本的DBA操作,因此了解一些基本的管理操作对他们来说很重要. 本文将讨论用 ...

  2. css吃豆人动画

    一. Css吃豆人动画 1. 上半圆:两个div,内部一个圆div,外部设置宽高截取半圆 外部div动画:animation: 动画样式 1s(时长) ease(动画先低速后快速) infinite( ...

  3. 数据分析之pandas库--series对象

    1.Series属性及方法 Series是Pandas中最基本的对象,Series类似一种一维数组. 1.生成对象.创建索引并赋值. s1=pd.Series() 2.查看索引和值. s1=Serie ...

  4. 实践:使用了CompletableFuture之后,程序性能提升了三倍

    CompletableFuture 相比于jdk5所提出的future概念,future在执行的时候支持异步处理,但是在回调的过程中依旧是难免会遇到需要等待的情况. 在jdk8里面,出现了Comple ...

  5. tensorflow开发环境版本组合

    记录下各模块的版本 tensorflow 1.15.0       print tf.__version__ cuda 10.0.130            nvcc -v cudnn 7.6.4  ...

  6. Jmeter 连接Redis获取数据集

    公司开展了新的业务活动,需要配合其他部门做压测,由于脚本中的手机号和用户的uid需要参数化而且每次均不能重复,最初的考虑使用csv的方式来获取数据,比较头疼的问题是集群节点需要维护测试数据,所以我将所 ...

  7. Matplotlib数据可视化(2):三大容器对象与常用设置

      上一篇博客中说到,matplotlib中所有画图元素(artist)分为两类:基本型和容器型.容器型元素包括三种:figure.axes.axis.一次画图的必经流程就是先创建好figure实例, ...

  8. toj 3616 Add number (没想到啊~~)

    Add number 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 60 测试通过: 21 描述 Employees of Baidu like ...

  9. Windows Server 2012 R2的安装(GUI桌面版本)

    镜像:cn_windows_server_2012_r2_x64_dvd_2707961.iso 1.将安装光盘插入服务器,开机会读取到Windows安装程序,点击下一步 2.点击现在安装     3 ...

  10. fastJson&edis

    fastJson&redis 1. fastJson 1.1 依赖 <dependency> <groupId>com.alibaba</groupId> ...