An array is an indexed collection of objects, all of the same type.
You declare an array by giving the type of objects the array
contains, followed by the square bracket operator ([]
), followed by the name of the array.
You then instantiate the array with the new
keyword and the number of elements the
array will contain.
The index of the first element in the array is always zero,
and the index of the last element in the array is always Length-1
.
You can use a for
loop to
iterate through the array, by using the loop’s counter as the index
to the array.
The foreach
statement
allows you to iterate through the items in the array (or any other
collection) without the need for a counter.
The elements of an array can be initialized when the array is
created by providing the values of the members in curly braces
({}
).
The params
keyword lets you
pass an arbitrary number of parameters of the same type into a
method; the method will treat the parameters as a single
array.
Arrays can contain more than one dimension. A two-dimensional array has two indexes, which you can think of as rows and columns.
A rectangular array is a two-dimensional array in which all the rows have the same number of columns.
A jagged array is an array of arrays—the rows do not need to be all the same length.
The Length
property of an
array returns the total number of elements in the array.
The array class contains a number of methods for searching, sorting, and manipulating the elements.