亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# - 索引器

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

语法

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

element-type this[int index] {

   // The get accessor.
   get {
      // return the value specified by index
   }
   
   // The set accessor.
   set {
      // set the value specified by index
   }
}

索引器的使用

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

定义属性涉及提供属性名称。索引器不是用名称定义的,而是用 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();
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

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

重载索引器

索引器可能会过载。索引器也可以用多个参数声明,每个参数可以是不同的类型。索引不必是整数。 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();
      }
   }
}

当上面的代码编译并执行时,它会产生以下结果 -

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

C语言

  1. C# Hello World - 你的第一个 C# 程序
  2. C# 关键字和标识符
  3. C# 变量和(原始)数据类型
  4. C# 运算符
  5. C# 位和位移运算符
  6. C# 基本输入和输出
  7. C# 表达式、语句和块(附示例)
  8. C# 注释
  9. C# switch 语句
  10. C# 三元 (? :) 运算符
  11. C# 中断语句
  12. C# continue 语句