Indexer based on switch statement : Indexer : Class C# Examples


C# Examples » Class » Indexer »

 

Indexer based on switch statement









    
using  System;


class  MyClass
{
      public  string  value0;                          
      public  string  value1;                        
      public  string  value2;                    
      
      public  string  this[int  index]  
      {
            set  
            {
                  switch  (index)
                  {
                        case  0:  value0  =  value;
                              break;
                        case  1:  value1  =  value;
                              break;
                        case  2:  value2  =  value;
                              break;
                  }
            }
            get  
            {
                  switch  (index)
                  {
                        case  0:  return  value0;
                        case  1:  return  value1;
                        case  2:  return  value2;
                        default:
                              return  "";
                  }
            }
      }
}

class  MainClass
{
      static  void  Main()
      {
            MyClass  myObject  =  new  MyClass();
            myObject.value0  =  "0";
            myObject.value1  =  "1";
            myObject.value2  =  "2";

            Console.WriteLine("{0},  {1},  {2}",  myObject[0],  myObject[1],  myObject[2]);
      }
}
    
   
  
   



Output

0, 1, 2


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Indexer