本笔记摘抄自: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. 如何构建OpenStack镜像

    本文以制作CentOS7.2镜像为例,详细介绍手动制作OpenStack镜像详细步骤,解释每一步这么做的原因.镜像上传到OpenStack glance,支持以下几个功能: 支持密码注入功能(nova ...

  2. docker容器互联,实现目录、服务共享

    一.需求 docker使服务之间实现容器隔离,比如Javaweb项目前端.后端.数据库.数据库后台,分别把它们部署在不同的容器里面,实现隔离.但服务和服务之间也有互访的需求,这就涉及到容器网络和容器互 ...

  3. 基于HttpURLConnection的接口调用,支持GET&POST

    单位要做一个多级部署平台,大概意思就是一级系统可以监控下属地域的数据也可管理本地的数据.之前做短信猫用过httpClient请求,与此大同小异.封装了一个两种请求方式的工具类. package com ...

  4. Caliburn.Micro框架之Action Convertions

    首先新建一个项目,名称叫Caliburn.Micro.ActionConvertions 然后删掉MainWindow.xaml 然后去app.xaml删掉StartupUri这行代码 其次,安装Ca ...

  5. [20200211]使用DBMS_SHARED_POOL.MARKHOT与sql_id的计算.txt

    [20200211]使用DBMS_SHARED_POOL.MARKHOT与sql_id的计算.txt --//以前写的,使用DBMS_SHARED_POOL.MARKHOT标记热的sql_id,这样相 ...

  6. sed命令简介

    sed处理时,有2个缓冲区:[pattern space]和[hold space] sed执行过程: 先读入一行,去掉尾部换行符,存入[pattern space],执行编辑命令. 处理完毕,除非加 ...

  7. hive中parquet存储格式数据类型timestamp的问题

    当存储格式为parquet 且 字段类型为 timestamp 且 数据用hive执行sql写入. 这样的字段在使用impala读取时会少8小时.建议存储为sequence格式或者将字段类型设置为st ...

  8. 小白的linux笔记3:对外联通——开通ssh和ftp和smb共享

    1.SSH的开通.https://www.cnblogs.com/DiDiao-Liang/articles/8283686.html 安装:yum install sshd或yum install ...

  9. window10 cmd 常见命令

    AT 计划在计算机上运行的命令和程序. ATTRIB 显示或更改文件属性. BREAK 设置或清除扩展式 CTRL+C 检查. CACLS 显示或修改文件的访问控制列表(ACLs). CALL 从另一 ...

  10. ArcGIS Runtime SDK for Android中SimpleFillSymbol.Style样式

    SimpleFillSymbol.Style样式枚举共8种: 1.BACKWARD_DIAGONAL 反对角线填充 2.CROSS 交叉线填充 3.DIAGONAL_CROSS 前后对角线填充 4.F ...