12.1. Understanding Indexer Methods

As a programmer, you are certainly familiar with the process of accessing individual items contained within a simple array using the index operator ([]), for example:

static void Main(string[] args)
{
  // Loop over incoming command line arguments
  // using index operator.
  for(int i = 0; i < args.Length; i++)
    Console.WriteLine("Args: {0}", args[i]);

  // Declare an array of local integers.
  int[] myInts = { 10, 9, 100, 432, 9874};

  // Use the index operator to access each element.
  for(int j = 0; j < myInts.Length; j++)
    Console.WriteLine("Index {0}  = {1} ", j,  myInts[j]);
  Console.ReadLine();
}

This code is by no means a major newsflash. However, the C# language provides the capability to design custom classes and ...

Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.