Skip to content

Indexer Methods

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision

Basic implementation.

public ReturnType this[int i]
{
  get { return (ReturnType)internalList[i]; }
  set { internalList[i] = value; }
}

Can use indexing by non-integer types too.

public ReturnType this[string s] {
  /* ... */
}

Multidimensional indexing.

public ReturnType this[string a, string b] {
  /* ... */
}

These can be overloaded so you can use multiple methods of indexing with your custom type.

You can also specify them in an interface.

public interface IMyContainer
{
  string this[int index] { get; set; }
}
Clone this wiki locally