Indexers and operator overloading

Indexers

Indexers are a syntactic convenience that enable you to create a classarrow-up-right, structarrow-up-right, or interfacearrow-up-right that client applications can access just as an array. Indexers are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array.

  • Indexers enable objects to be indexed in a similar manner to arrays.

  • A get accessor returns a value. A set accessor assigns a value.

  • The thisarrow-up-right keyword is used to define the indexer.

  • The valuearrow-up-right keyword is used to define the value being assigned by the set indexer.

  • Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.

  • Indexers can be overloaded.

  • Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.

public int this[int index]   // Indexer declaration  
{
    // get and set accessors  
}  

Indexers are like properties. Except for the differences shown in the following table, all the rules that are defined for property accessors apply to indexer accessors also.

Property

Indexer

Allows methods to be called as if they were public data members.

Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.

Accessed through a simple name.

Accessed through an index.

Can be a static or an instance member.

Must be an instance member.

A getarrow-up-right accessor of a property has no parameters.

A get accessor of an indexer has the same formal parameter list as the indexer.

A setarrow-up-right accessor of a property contains the implicit value parameter.

A set accessor of an indexer has the same formal parameter list as the indexer, and also to the valuearrow-up-right parameter.

Supports shortened syntax with Auto-Implemented Propertiesarrow-up-right.

Does not support shortened syntax.

Operator overloading

A user-defined type can overload a predefined C# operator. That is, a type can provide the custom implementation of an operation when one or both operands are of that type. The Overloadable operatorsarrow-up-right section shows which C# operators can be overloaded.

Use the operator keyword to declare an operator. An operator declaration must satisfy the following rules:

  • It includes both a public and a static modifier.

  • A unary operator takes one parameter. A binary operator takes two parameters. In each case, at least one parameter must have type T or T? where T is the type that contains the operator declaration.

The following table provides information about overloadability of C# operators:

Operators

Overloadability

These binary operators can be overloaded. Certain operators must be overloaded in pairs; for more information, see the note that follows this table.

Conditional logical operators cannot be overloaded. However, if a type with the overloaded true and falseoperatorsarrow-up-right also overloads the & or | operator in a certain way, the && or || operator, respectively, can be evaluated for the operands of that type. For more information, see the User-defined conditional logical operatorsarrow-up-rightsection of the C# language specificationarrow-up-right.

Element access is not considered an overloadable operator, but you can define an indexerarrow-up-right.

The cast operator cannot be overloaded, but you can define new conversion operators. For more information, see User-defined conversion operatorsarrow-up-right.

Compound assignment operators cannot be explicitly overloaded. However, when you overload a binary operator, the corresponding compound assignment operator, if any, is also implicitly overloaded. For example, += is evaluated using +, which can be overloaded.

circle-info

The comparison operators must be overloaded in pairs. That is, if either operator of a pair is overloaded, the other operator must be overloaded as well. Such pairs are as follows:

  • == and != operators

  • < and > operators

  • <= and >= operators

Last updated