Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
Arrays
 Single-Dimensional Arrays
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Programming Guide
Single-Dimensional Arrays (C# Programming Guide)

You can declare an array of five integers as in the following example:

C#
int[] array = new int[5];

This array contains the elements from array[0] to array[4]. 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:

C#
string[] stringArray = new string[6];

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:

C#
int[] array1 = new int[5] { 1, 3, 5, 7, 9 };

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:

C#
string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

When you initialize an array upon declaration, it is possible to use the following shortcuts:

C#
int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

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#
int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 };   // OK
//array3 = {1, 3, 5, 7, 9};   // Error

Consider the following array declaration:

C#
SomeType[] array4 = new SomeType[10];

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 results in creating 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 on value types and reference types, see Types (C# Reference).

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Must initialize all elements in initialization list      David M. Kean - MSFT ... Phil Zhou   |   Edit   |   Show History

Unlike in C/C++, initialization lists, if specified along with the rank specifier, must initialize all elements of the array.

For example, the following declaration results in a compilation error:

[C#]
  
int[] values = new int[3] { 1, 2 };    // CS0178 Invalid rank specifier: expected ',' or ']'

Whereas the following compiles without error:

[C#]
  
int[] values = new int[3] { 1, 2, 3 }; // OK
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker