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

索引器和数组比较:

(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["张三", ] = "人事部";
Info["李四", ] = "行政部";
Console.WriteLine(Info["张三", ]);
Console.WriteLine(Info["李四", ]); Console.WriteLine(); //this[int num]的使用
foreach (EntrantInfo en in Info[])
{
Console.WriteLine(en.Name);
Console.WriteLine(en.Department);
}
}
}

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

  1. C#索引器学习笔记

    本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用. 索引器允许类或者结构的实 ...

  2. 《Inside C#》笔记(六) 属性、数组、索引器

    一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...

  3. C#学习笔记(十六):索引器和重载运算符

    二维数组如何映射到一维数组 重载运算符 1.算术运算符 2.关系运算符, < 和 > 成对重载 using System; using System.Collections.Generic ...

  4. 接口、索引器、Foreach的本质(学习笔记)

    接口 什么是接口? 接口代表一种能力,和抽象类类似但比抽象类的抽象程度更高! 接口的定义: public interface IEat//定义一个接口 { void Eat(string food); ...

  5. C# 索引器(C#学习笔记05)

    索引器 索引器能够使对象像数组一样被索引,使用数组的访问方式 object[x] 索引器的声明在某种程度上类似于属性的声明,例如,使用 get 和 set 方法来定义一个索引器. 不同的是,属性值的定 ...

  6. 【c# 学习笔记】索引器

    当一个类包含数组成员时,索引器 的使用将大大地简化对类中数组成员的访问.索引器的定义类似于属性,也具有GET访问器和set访问器,如下: [修饰符] 数据类型 this[索引类型 index] { g ...

  7. Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板

    原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...

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

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

  9. C# 索引器,实现IEnumerable接口的GetEnumerator()方法

    当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...

随机推荐

  1. BZOJ5343 & 洛谷4602 & LOJ2555:[CTSC2018]混合果汁——题解

    https://www.luogu.org/problemnew/show/P4602 https://loj.ac/problem/2555 https://www.lydsy.com/JudgeO ...

  2. 洛谷4578 & LOJ2520:[FJOI2018]所罗门王的宝藏——题解

    https://www.luogu.org/problemnew/show/P4578 https://loj.ac/problem/2520 有点水的. 先转换成图论模型,即每个绿宝石,横坐标向纵坐 ...

  3. BZOJ1046 [HAOI2007]上升序列 【LIS + 字典序最小】

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 5410  Solved: 1877 [Submit][St ...

  4. 洛谷 P4735 最大异或和 解题报告

    P4735 最大异或和 题目描述 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的 ...

  5. 弱校的ACM奋斗史

    这是一篇老文章,不过由于无法找到最初的发文地址,这里就不能粘贴原文网址了.本站转载此文与ACMer们共勉.感谢acmerfight供稿. 题解:还记得2年前的一个晚上,我和一个女孩一起写完了这篇文章. ...

  6. 一个简单的适用于Vue的下拉刷新,触底加载组件

    一个简单的适用于Vue的上拉刷新,触底加载组件,没有发布npm需要时直接粘贴定制修改即可 <template> <div class="list-warp-template ...

  7. JQuery选择符的理解与应用

    JQuery强大的选择符可以让我们获得页面中任何元素进行操作,并且使用简单方便,可读性强.本章内容根据本人在开发中常用到的选择符作为例子来进行讲解,如有更多常用的简单的例子可回复提供,参与讨论,一起学 ...

  8. xshell如何让窗口并列排序

    如图所示:

  9. [solr]solr的安装

    solr是什么? 翻译: SolrTM is the popular, blazing fast open source enterprise search platform from the Apa ...

  10. OpecnCV训练分类器详细整理

    本文主要是对下面网址博客中内容的实例操作: http://blog.csdn.net/byxdaz/article/details/4907211 在上述博客中,详细的讲述了OpenCV训练分类器的做 ...