索引器

一、索引器的意义

  当一个类中包含数组成员时,索引器可很方便的对数组成员的访问。

二、方法

  [修饰符] 数据类型 this [索引类型 index]

       {

     get{返回类中数组某个元素}

                set{对类中数组元素赋值}

  }

     实例:

 

 1     public class Indexer
 2     {
 3         private int[] intarray = new int[10];
 4 
 5         //定义索引器
 6         public int this[int index]
 7         {
 8             get { return intarray[index]; }
 9             set { intarray[index] = value; }
10         }
11     }
12     
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             Indexer indexer = new Indexer();
18             indexer[0] = 1;
19             indexer[1] = 2;
20             indexer[2] = 3;
21 
22             Console.WriteLine(indexer[0]);
23             Console.WriteLine(indexer[1]);
24             Console.WriteLine(indexer[2]);
25             Console.Read();
26         }
27     }
原文地址:https://www.cnblogs.com/goldendragon/p/10074913.html