概念

索引器是一种特殊的类的成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写。

当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作。

通过索引器可以存取类的实例的数组成员,操作方法和数组相似。

索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。

定义

定义索引器的方式与定义属性有些类似,形式如下

  【修饰符】数据类型 this[索引类型 index]

  {

  get{//获取属性的代码}

  set{//设置属性的代码}

  }

使用

对象名[索引];

  • 其中索引的数据类型必须与索引器的索引类型相同

比较

索引器与数组的区别

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

(2)索引器允许重载

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

 

索引器和属性的不同点

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

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

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

 

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace suoyinqi
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 p = new Class1();
            p[0] = \"唐\";
            p[1] = \"宋\";
            p[2] = \"元\";
            p[3] = \"明\";
            p[4] = \"清\";
            Console.WriteLine(p[0]+p[1]+p[2]+p[3]+p[4]);

            Console.ReadKey();
        }
    }
    class Class1
    {
        //数组
        string[] arr = new string[5];

        //索引器
        public string this[int index]
        {
            get { return arr[index]; }
            set { arr[index] = value; }
        }
    }
}

--->
唐宋元明清
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace syq
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 p = new Class1();
            p[\"强\"] = \"唐\";
            p[\"弱\"] = \"宋\";
            p[\"蛮\"] = \"元\";
            p[\"文\"] = \"明\";
            p[\"满\"] = \"清\";
            Console.WriteLine(p[\"强\"] + p[\"弱\"] + p[\"蛮\"] + p[\"文\"] + p[\"满\"]);
            Console.ReadKey();
        }
    }

    class Class1
    {
        //数组
        string[] arr = new string[5];
        //索引器
        public string this[string index]
        {
            get { return arr[int.Parse(index)]; }
            set { arr[int.Parse(index)] = value; }
        }
    }
}

--->
唐宋元明清
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ssyyqq
{
    class Program
    {
        static void Main(string[] args)
        {

            chao ch = new chao();
            ch[\"禹\"] = \"夏\";
            ch[\"汤\"] = \"商\";
            ch[\"昌\"] = \"周\";
            
            Console.WriteLine(ch[\"禹\"]+ ch[\"汤\"]+ ch[\"昌\"]);
            Console.ReadKey();
        }
    }

    class chao
    {
        //字典
        Dictionary<string, string> di = new Dictionary<string, string>();

        //索引器
        public string this [string index]
        {
            get { return di[index]; }
            set { di[index] = value; }
        }

    }
}

--->
夏商周
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mlist
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 class1 = new Class1();

            class1[0] = \"aaa\";
            class1[\"1\"] = \"bbb\";
            class1[2] = \"ccc\";

            Console.WriteLine(class1[0]);
            Console.WriteLine(class1[\"1\"]);
            Console.WriteLine(class1[2]);

            Console.ReadKey();
        }
    }

    class Class1
    {
        public List<string> list = new List<string>(3) {\"aa\",\"bb\",\"cc\" };
       
        public string this[int index]
        {
            get { return list[index]; }
            set { list[index] = value; }
        }

        public string this[string index]
        {
            get { return list[int.Parse(index)]; }
            set { list[int.Parse(index)] = value; }
        }
    }
}

--->
aaa
bbb
ccc

 

收藏 打印