Export (0) Print
Expand All
58 out of 90 rated this helpful - Rate this topic

Single-Dimensional Arrays

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

int[] myArray = new int [5];

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

string[] myStringArray = new string[6];

Array Initialization

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:

int[] myArray = new int[] {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:

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

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

int[] myArray = {1, 3, 5, 7, 9};
string[] weekDays = {"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};

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:

int[] myArray;
myArray = new int[] {1, 3, 5, 7, 9};   // OK
myArray = {1, 3, 5, 7, 9};   // Error

Value Type and Reference Type Arrays

Consider the following array declaration:

MyType[] myArray = new MyType[10];

The result of this statement depends on whether MyType 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 MyType. If MyType 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.

Passing Arrays as Parameters

You can pass an initialized array to a method. For example:

PrintArray(myArray);

You can also initialize and pass a new array in one step. For example:

PrintArray(new int[] {1, 3, 5, 7, 9});

Example

In the following example, a string array is initialized and passed as a parameter to the PrintArray method, where its elements are displayed:

// cs_sd_arrays.cs
using System;
public class ArrayClass 
{
   static void PrintArray(string[] w) 
   {
      for (int i = 0 ; i < w.Length ; i++) 
         Console.Write(w[i] + "{0}", i < w.Length - 1 ? " " : "");
      Console.WriteLine();
   }

   public static void Main() 
   {
      // Declare and initialize an array:
      string[] WeekDays = new string []
         {"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};

      // Pass the array as a parameter:
      PrintArray(WeekDays);
   }
}

Output

Sun Sat Mon Tue Wed Thu Fri

See Also

Arrays | Multidimensional Arrays | Jagged Arrays

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.