Share via


How to: Declare an Array

This example shows three different ways to declare different kinds of arrays: single-dimensional, multidimensional, and jagged.

Example

// Single-dimensional arrays.
int[] myArray1 = new int [5];
string[] myArray2 = new string[6];

// Multidimensional arrays.
int[,] myArray3 = new int[4,2];
int[,,] myArray4 = new int [4,2,3];

// Jagged array (array of arrays)
int[][] myArray5 = new int[3][];

Compiling the Code

Copy the code and paste it into the Main method of a console application.

Robust Programming

Make sure you initialize the elements of the jagged array before using it, as follows:

myArray5[0] = new int[7];
myArray5[1] = new int[5];
myArray5[2] = new int[2];

See Also

Concepts

C# Language Primer

Arrays and Collections

Other Resources

Visual C# Express