1 // cs_keyword_indexers.cs
2 using System;
3 class IndexerClass
4 {
5    private int [] myArray = new int[100];
6    public int this [int index]   // indexer declaration
7    {
8       get
9       {
10          // Check the index limits
11          if (index < 0 || index >= 100)
12             return 0;
13          else
14             return myArray[index];
15       }
16       set
17       {
18          if (!(index < 0 || index >= 100))
19             myArray[index] = value;
20       }
21    }
22 }
23 
24 public class MainClass
25 {
Main()26    public static void Main()
27    {
28       IndexerClass b = new IndexerClass();
29       // call the indexer to initialize the elements #3 and #5:
30       b[3] = 256;
31       b[5] = 1024;
32       for (int i=0; i<=10; i++)
33       {
34          Console.WriteLine("Element #{0} = {1}", i, b[i]);
35       }
36    }
37 }
38