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。

给TA打赏
共{{data.count}}人
人已打赏
C#

C# 委托(delegate)

2022-8-25 17:37:44

C#

C# 反射(Reflection)

2022-8-25 19:12:52

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索