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; /*  j  a v a2 s  .  co m*/
 
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   j  a  v a 2s .  c  o m*/

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; //from j a v  a2 s.c om
 
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 class properties
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances