站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

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 {
}
    [ directory ] Previous Section Next Section