Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A Class A with an indexer.Lecture 5 - slide 13 : 29
Program 1
using System;

public class A {
  private double d, e, f;   

  public A(double v){
    d = e = f = v;          
  }

  public double this [int i]{
   get {                 
     switch (i){
       case 1: {return d;}     
       case 2: {return e;}     
       case 3: {return f;}     
       default: throw new Exception("Error");
     }
   }
   set {
     switch (i){
       case 1: {d = value; break;}   
       case 2: {e = value; break;}   
       case 3: {f = value; break;}   
       default: throw new Exception("Error");
    }
   }
  }             

  public override string ToString(){
    return "A: " + d + ", " + e + ", " + f;
  } 

}
 
 
 
Three private instance variables
 
 
... all initialized to the same value
 
 
 
 
 
anAObject[1] returns d
anAObject[2] returns e
anAObject[3] returns f
 
 
 
 
 
anAObject[1] = ... assigns d
anAObject[2] = ... assigns e
anAObject[3] = ... assigns f