良许Linux教程网 干货合集 讲解一下C# 索引器(Indexer)

讲解一下C# 索引器(Indexer)

索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。

了解下C# 索引器(Indexer)了解下C# 索引器(Indexer)

当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ] 来访问该类的的成员。

语法

一维索引器的语法如下:

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

  // set 访问器
  set
  {
     // 设置 index 指定的值
  }
}

索引器(Indexer)的用途

索引器的行为的声明在某种程度上类似于属性(property)。就像属性(property),您可使用 get 和 set 访问器来定义索引器。但是,属性返回或设置一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。换句话说,它把实例数据分为更小的部分,并索引每个部分,获取或设置每个部分。

定义一个属性(property)包括提供属性名称。索引器定义的时候不带有名称,但带有 this 关键字,它指向对象实例。下面的实例演示了这个概念:

实例

using System;
namespace IndexerApplication
{
  class IndexedNames
  {
     private string[] namelist = new string[size];
     static public int size = 10;
     public IndexedNames()
     {
        for (int i = 0; i "N. A.";
     }
     public string this[int index]
     {
        get
        {
           string tmp;

           if( index >= 0 && index else
           {
              tmp = "";
           }

           return ( tmp );
        }
        set
        {
           if( index >= 0 && index "Zara";
        names[1] = "Riz";
        names[2] = "Nuha";
        names[3] = "Asif";
        names[4] = "Davinder";
        names[5] = "Sunil";
        names[6] = "Rubic";
        for ( int i = 0; i 

当上面的代码被编译和执行时,它会产生下列结果:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.

重载索引器(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 "N. A.";
        }
     }
     public string this[int index]
     {
        get
        {
           string tmp;

           if( index >= 0 && index else
           {
              tmp = "";
           }

           return ( tmp );
        }
        set
        {
           if( index >= 0 && index while(index if (namelist[index] == name)
              {
               return index;
              }
              index++;
           }
           return index;
        }

     }

     static void Main(string[] args)
     {
        IndexedNames names = new IndexedNames();
        names[0] = "Zara";
        names[1] = "Riz";
        names[2] = "Nuha";
        names[3] = "Asif";
        names[4] = "Davinder";
        names[5] = "Sunil";
        names[6] = "Rubic";
        // 使用带有 int 参数的第一个索引器
        for (int i = 0; i "Nuha"]);
        Console.ReadKey();
     }
  }
}

当上面的代码被编译和执行时,它会产生下列结果:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部