Previous Page
Next Page

Chapter 15 Quick Reference

To

Do this

Create an indexer for a class or struct

Declare the type of the indexer, followed by the keyword this, and then the indexer arguments between square brackets. The body of the indexer can contain a get and/or set accessor. For example:

struct RawInt
{
    ...
    public bool this [ int index  ]
    {
        get { ... }
        set { ... }
    }
    ...
}

Define an indexer in an interface

Define an indexer with the get and/or set keywords. For example:

interface IRawInt
{
    bool this [ int index ]  get;  set; }
}

Implement an interface indexer in a class or struct

In the class or struct that implements the interface, define the indexer and implement the accessors. For example:

struct RawInt : IRawInt
{
    ...
    public bool this [ int index  ]
    {
        get { ... }
        set { ... }
    }
    ...
}

Implement an interface indexer by using explicit interface implementation in a class or struct

In the class or struct that implements the interface, explicitly name the interface, but do not specify the indexer accessibility. For example:

struct RawInt : IRawInt
{
    ...
    bool IRawInt.this [ int index  ]
    {
        get { ... }
        set { ... }
    }
    ...
}

Previous Page
Next Page