8.11. The Indexing Operator

Square brackets ([]) are used for arrays, indexers, and attributes. They can also be used with pointers. An array type is a type followed by []:

int[]golonghorns; // golonghorns is of type int[], "array of int"
golonghorns = new int[100];  // create a 100-element int array

An array of golonghorns is accessed using the [] operator. The array indexing operator cannot be overloaded; however, types can define indexers: properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indices, but indexer parameters can be declared to be of any type. For example, here is a Hashtable type, which associates keys and values of arbitrary type:

 Collections.Hashtable h = new Collections.Hashtable(); ...

Get .NET for Java Developers: Migrating to C# 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.