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();
h["a"] = 123; // note: using a string as the index

The [] operator is also used to define attributes:

[attribute(AllowMultiple=true)]
public class Attr {
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.59