|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Single-Dimensional Arrays (C# Programming Guide)
You can declare an array of five integers as in the following example:
This array contains the elements from array to array. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero.
An array that stores string elements can be declared in the same way. For example:
It is possible to initialize an array upon declaration, in which case, the rank specifier is not needed because it is already supplied by the number of elements in the initialization list. For example:
A string array can be initialized in the same way. The following is a declaration of a string array where each array element is initialized by a name of a day:
When you initialize an array upon declaration, you can use the following shortcuts:
It is possible to declare an array variable without initialization, but you must use the new operator when you assign an array to this variable. For example:
C# 3.0 introduces implicitly typed arrays. For more information, see Implicitly Typed Arrays (C# Programming Guide).
Consider the following array declaration:
The result of this statement depends on whether SomeType is a value type or a reference type. If it is a value type, the statement creates an array of 10 instances of the type SomeType. If SomeType is a reference type, the statement creates an array of 10 elements, each of which is initialized to a null reference.
For more information about value types and reference types, see Types (C# Reference).