A two-dimensional indexer : Two Dimensional Indexer « Class « C# / CSharp Tutorial






using System; 
 
class MyArray2D {  
  public MyArray2D(int r, int c) { 
  } 
 
  // This is the indexer for MyArray2D. 
  public int this[int index1, int index2] { 
    // This is the get accessor. 
    get { 
        return index1 + index2; 
    } 
  } 
}  
  
class MainClass {  
  public static void Main() {  
    MyArray2D myArray = new MyArray2D(3, 5); 
    int x; 
 
    for(int i=0; i < 6; i++) { 
      x = myArray[i,i]; 
      if(x != -1) Console.Write(x + " "); 
    } 
    Console.WriteLine(); 
 
  } 
}
0 2 4 6 8 10








7.41.Two Dimensional Indexer
7.41.1.A two-dimensional indexer