C# 索引器

C# 中,索引器(Indexer) 对象可以像数组一样通过索引访问数据。这种功能通常用于封装集合或数组,使得类的实例可以像数组一样被访问。C# 索引器是一种强大的语言特性,它为类提供了自定义的下标访问机制。通过使用索引器,可以更直观、更简洁地访问和操作类内部的数据,同时保持了类的封装性和类型安全性。在需要以类似数组或字典的方式访问类的数据时,索引器是一个非常有用的工具。

1、索引器定义语法

element-type this[int index]
{
   // get 访问器
   get
   {
      // 返回 index 指定的值
   }
   // set 访问器
   set
   {
      // 设置 index 指定的值
   }
}

索引器允许类或者结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于索引器访问需要参数。

例如,

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         namelist[i] = "cjavapy";
      }
      public string this[int index]
      {
         get
         {
            string tmp;
            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }
            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "C";
         names[1] = "Java";
         names[2] = "Python";
         names[3] = "JavaScript";
         names[4] = "Csharp";
         names[5] = "CPP";
         for ( int i = 0; i < IndexedNames.size; i++ )
         {
            Console.WriteLine(names[i]);
         }
         Console.ReadKey();
      }
   }
}

2、索引器重载(Indexer)

索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,如字符串类型。

例如,

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         {
          namelist[i] = "cjavapy";
         }
      }
      public string this[int index]
      {
         get
         {
            string tmp;
            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }
            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      public int this[string name]
      {
         get
         {
            int index = 0;
            while(index < size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }
      }
      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "C";
         names[1] = "Java";
         names[2] = "Python";
         names[3] = "JavaScript";
         names[4] = "CSharp";
         names[5] = "CPP";
         // 使用带有 int 参数的第一个索引器
         for (int i = 0; i < IndexedNames.size; i++)
         {
            Console.WriteLine(names[i]);
         }
         // 使用带有 string 参数的第二个索引器
         Console.WriteLine(names["CSharp"]);
         Console.ReadKey();
      }
   }
}

3、索引器与数组的区别

1)索引器的索引值(Index)类型不限定为整数,用来访问数组的索引值(Index)一定为整数,而索引器的索引值类型可以定义为其他类型。

2)索引器可以重载,一个类不限定为只能定义一个索引器,只要索引器的函数签名不同,就可以定义多个索引器,可以重载它的功能。

3)索引器不是一个变量,索引器没有直接定义数据存储的地方,而数组有。索引器具有Get和Set访问器。

4、索引器与属性的区别

1)索引器以函数签名方式 this 来标识,而属性采用名称来标识,名称可以任意。

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

3)索引器不能用static 来进行声明,而属性可以。索引器永远属于实例成员,因此不能声明为static

推荐阅读
cjavapy编程之路首页