Csharp/CSharp Tutorial/Class/Two Dimensional Indexer

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A two-dimensional indexer

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