资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#中索引器的详细介绍

今天小编就为大家带来一篇C#中索引器的详细介绍的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。

网站的建设创新互联建站专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为成都建筑动画等企业提供专业服务。

索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便、直观的被引用。索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用。定义了索引器的类可以让您像访问数组一样的使用 [ ] 运算符访问类的成员。(当然高级的应用还有很多,比如说可以把数组通过索引器映射出去等等)

索引器允许类或结构的实例就像数组一样进行索引。索引器类似于属性,不同之处在于它们的取值函数采用参数。索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的。

索引器概述

  • 使用索引器可以用类似于数组的方式为对象建立索引。

  • get 取值函数返回值。 set 取值函数分配值。

  • this 关键字用于定义索引器。

  • value 关键字用于定义由 set 索引器分配的值。

  • 索引器不必根据整数值进行索引;由你决定如何定义特定的查找机制。

  • 索引器可被重载。

  • 索引器可以有多个形参,例如当访问二维数组时。

索引器和数组比较:

(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#中的索引器大概了解了吗?如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


标题名称:C#中索引器的详细介绍
本文链接:http://cdkjz.cn/article/posdgi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220