C# Indexer

In this chapter you will learn:

  1. What is an Indexer
  2. How to create an Indexer
  3. Example for creating an indexer
  4. How to create a getter only indexer

Description

Indexers can access elements in a class or struct that encapsulate a list or dictionary of values.

Indexers are similar to properties, but are accessed via an index argument rather than a property name.

The string class has an indexer that lets you access each of its char values via an int index:


string s = "hello"; 
Console.WriteLine (s[0]); // 'h' 
Console.WriteLine (s[3]); // 'l' 

The syntax for using indexers is like that for using arrays when the index is an integer type.

Syntax

We can use the following syntax to create an indexer.


public type this [int arg1] 
{ 
  get { ... }  set { ... } 
} 

A type may declare multiple indexers, each with parameters of different types. An indexer can also take more than one parameter:


public type this [int arg1, string arg2] 
{ 
  get { ... }  set { ... } 
} 

If you omit the set accessor, an indexer becomes read-only.

Example

To write an indexer, define a property called this, specifying the arguments in square brackets. For instance:

The following code uses the indexer to access each week day.


using System;/*from   ww  w. j  a va 2  s  .  c  o m*/
class Week
{
    string[] days = new string[] { "Monday", "Tuesday", "Wednesday", 
                    "Thursday", "Friday", "Saturday", "Sunday" };

    public string this[int i]
    {
        get
        {
            return days[i];
        }
        set
        {
            days[i] = value;
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        Week w = new Week();

        Console.WriteLine(w[2]);


    }
}

The output:

Example 2

The Employee class in the following code only has a getter indexer since it only implements the getter part of an indexer.


using System;//w  ww  . ja va  2 s. c  om

public class Employee
{
  private string firstName;
  private string lastName;

  public Employee(string firstName, string lastName)
  {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public string this[int index]
  {
    get
    {
      switch (index)
      {
        case 0:
          return firstName;
        case 1:
          return lastName;
        default:
          throw new IndexOutOfRangeException();
      }
    }
  }

}

class MainClass
{
  public static void Main()
  {
    Employee myEmployee = new Employee("T", "M");

    Console.WriteLine("myEmployee[0] = " + myEmployee[0]);
    Console.WriteLine("myEmployee[1] = " + myEmployee[1]);

  }

}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Indexing with an String Indexer
  2. Define both int and string indexer for a class
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