C# Indexer logic

In this chapter you will learn:

  1. How to write more complex code for indexer
  2. Indexer based on 0 or non-zero
  3. Use an indexer to create a fail-soft array

Indexer with more logic

Indexers don't have to operate on actual arrays


using System; /*from w w w. j  av a  2  s .  c  om*/
 
class MySequence {  
 
  public int this[int index] { 
    get { 
      return index + 1; 
    } 
 
  } 
}  
  
class MainClass {  
  public static void Main() {  
    MySequence sequence = new MySequence(); 
 
    for(int i=0; i < 8; i++) 
      Console.Write(sequence[i] + " "); 
    Console.WriteLine(); 
  } 
}

The code above generates the following result.

Indexer based on 0 or non-zero


using System;/*from   ww w .  j a v  a2  s.com*/

class MyClass
{
   int value0; 
   int value1; 
   public int this[int index] 
   {
      get
      {
         return (0 == index) ? value0 : value1;
      }
      set
      {
         if (0 == index)
            value0 = value; 
         else
            value1 = value; 
      }
   }
}

class MainClass
{
   static void Main()
   {
      MyClass a = new MyClass();
      Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]);
      a[0] = 15;
      a[1] = 20;
      Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]);
   }
}

The code above generates the following result.

Use an indexer to create a fail-soft array


using System; //w  w  w .  java  2  s  .c o m
 
class MyArray {  
  int[] a;
 
  public int Length; 
 
  public bool errflag;
   
  public MyArray(int size) { 
    a = new int[size]; 
    Length = size;  
  } 
 
  // This is the indexer for MyArray. 
  public int this[int index] { 
    get { 
      if(indexCheck(index)) { 
        errflag = false; 
        return a[index]; 
      } else { 
        errflag = true; 
        return 0; 
      } 
    } 
 
    set { 
      if(indexCheck(index)) { 
        a[index] = value; 
        errflag = false; 
      } 
      else errflag = true; 
    } 
  } 
 

  private bool indexCheck(int index) { 
   if(index >= 0 & index < Length) 
      return true; 
   return false; 
  } 
}  
  
class MainClass {  
  public static void Main() {  
    MyArray myArray = new MyArray(5); 
    int x; 
 
    Console.WriteLine("Fail quietly."); 
    for(int i=0; i < 10; i++) 
      myArray[i] = i*10; 
 
    for(int i=0; i < 10; i++) { 
      x = myArray[i]; 
      if(x != -1) Console.Write(x + " "); 
    } 
    Console.WriteLine(); 
 
    Console.WriteLine("\nFail with error reports."); 
    for(int i=0; i < 10; i++) { 
      myArray[i] = i*10; 
      if(myArray.errflag) 
        Console.WriteLine("myArray[" + i + "] out-of-bounds"); 
    } 
 
    for(int i=0; i < 10; i++) { 
      x = myArray[i]; 
      if(!myArray.errflag) 
         Console.Write(x + " "); 
      else 
         Console.WriteLine("myArray[" + i + "] out-of-bounds"); 
    } 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is C# Constructor
  2. How to create a constructor
  3. Example
  4. How to define a constructor with parameters
Home »
  C# Tutorial »
    C# Types »
      C# Indexer
C# Indexer
C# String type indexer
C# Multidimensional indexer
C# Indexer overloading
C# Appendable Indexer
C# Indexer logic