Arrays (C# Programming Guide)
You can store multiple variables of the same type in an array data structure. You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object
as its type. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object.
type[] arrayName;
Example
The following example creates single-dimensional, multidimensional, and jagged arrays:
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array of 5 integers.
int[] array1 = new int[5];
// Declare and set array element values.
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax.
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array.
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values.
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array.
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure.
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}
Array overview
An array has the following properties:
- An array can be single-dimensional, multidimensional or jagged.
- The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.
- The default values of numeric array elements are set to zero, and reference elements are set to
null
. - A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to
null
. - Arrays are zero indexed: an array with
n
elements is indexed from0
ton-1
. - Array elements can be of any type, including an array type.
- Array types are reference types derived from the abstract base type Array. All arrays implement IList, and IEnumerable. You can use the foreach statement to iterate through an array. Single-dimensional arrays also implement IList<T> and IEnumerable<T>.
Default value behaviour
- For value types, the array elements are initialized with the default value, the 0-bit pattern; the elements will have the value
0
. - All the reference types (including the non-nullable), have the values
null
. - For nullable value types,
HasValue
is set tofalse
and the elements would be set tonull
.
Arrays as Objects
In C#, arrays are actually objects, and not just addressable regions of contiguous memory as in C and C++. Array is the abstract base type of all array types. You can use the properties and other class members that Array has. An example of this is using the Length property to get the length of an array. The following code assigns the length of the numbers
array, which is 5
, to a variable called lengthOfNumbers
:
int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = numbers.Length;
The Array class provides many other useful methods and properties for sorting, searching, and copying arrays. The following example uses the Rank property to display the number of dimensions of an array.
class TestArraysClass
{
static void Main()
{
// Declare and initialize an array.
int[,] theArray = new int[5, 10];
System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);
}
}
// Output: The array has 2 dimensions.
See also
- How to use single-dimensional arrays
- How to use multi-dimensional arrays
- How to use jagged arrays
- Using foreach with arrays
- Passing arrays as arguments
- Implicitly typed arrays
- C# Programming Guide
- Collections
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
Feedback
Submit and view feedback for