C# - 索引器

C# - 索引器 首页 / C#入门教程 / C# - 索引器

索引器允许对象(如数组)进行索引,当您为类定义索引器时,此类的行为类似于虚拟数组,然后可以使用数组访问运算符([])访问此类的。

一维索引器具有以下语法-

无涯教程网

element-type this[int index] {

   //The get accessor.
   get {
      //返回 index 指定的值
   }
   
   //The set accessor.
   set {
      //设置 index 指定的值
   }
}

Indexers索引

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

索引器不是使用名称定义的,而是使用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 < size; i++)
         namelist[i] = "N. A.";
      }
      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] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         
         for ( int i = 0; i < IndexedNames.size; i++ ) {
            Console.WriteLine(names[i]);
         }
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-indexers.html

来源:LearnFk无涯教程网

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

Indexers索引重载

索引器也可以用多个参数声明,每个参数可以是不同的类型,索引不必是整数,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] = "N. A.";
         }
      }
      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] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         
         //using the first indexer with int parameter
         for (int i = 0; i < IndexedNames.size; i++) {
            Console.WriteLine(names[i]);
         }
         
         //using the second indexer with the string parameter
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-indexers.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

编译原理之美 -〔宫文学〕

JavaScript核心原理解析 -〔周爱民〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

容器实战高手课 -〔李程远〕

技术面试官识人手册 -〔熊燚(四火)〕

说透数字化转型 -〔付晓岩〕

React Native 新架构实战课 -〔蒋宏伟〕

大厂设计进阶实战课 -〔小乔〕

现代React Web开发实战 -〔宋一玮〕

好记忆不如烂笔头。留下您的足迹吧 :)