Array Types

Array types are used for representing arrays with an underlying type being an internal type.

For valid internal types, array types can be declared using the type[] syntax as follows:

1
2
3
int[] arr1;    // Array of 'int' values
string[] arr2; // Array of 'string' values
bool[] arr3;   // Array of 'bool' values

By using array types, the compiler guarantees the elements of the array will be restricted to the declared underlying type.

External Arrays

The syntax for array types can only be used for internal types. For external types, there is no special syntax. Externally-typed arrays will fit into variables declared using var, function, or external:

1
2
3
4
var foo = [ "string", 100, false ];
function bar() {
    return [ true, 1 ];
}

Internal Arrays with External Elements

In order to explicitly declare an internally-typed array with external elements, use the System.Array class:

1
System.Array<external> foo = [ true, 1, "string" ];

An internally-typed array with external elements enables compile-time checking and analysis. For example, the compiler can ensure only valid array methods are called.

Arrays of Arrays

Arrays of arrays (jagged arrays) can be declared with the following syntax:

1
int[][] arr;

In order to access elements of the jagged arrays above:

1
arr[0][1]; // Accesses the second element (at index 1) of the first array inside the 'arr' array

See Also

Share

HTML | BBCode | Direct Link