Arrays and Collections

Storing groups of related items of data is a basic requirement of most software applications; the two primary ways of doing this are with arrays and collections.


Arrays are collections of objects of the same type. Because arrays can be almost any length, they can be used to store thousands or even millions of objects, but the size must be decided when the array is created. Each item in the array is accessed by an index, which is just a number that indicates the position or slot where the object is stored in the array. Arrays can be used to store both Reference Types and Value Types.

Single-Dimensional Arrays

An array is an indexed collection of objects. A single-dimensional array of objects is declared like this:

type[] arrayName;

Often, you initialize the elements in the array at the same time, like this:

int[] array = new int[5];

The default value of numeric array elements is zero, and reference elements default to null, but you can initialize values during array creation, like this:

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

Or even like this:

int[] array2 = {1, 3, 5, 7, 9};

Arrays use zero-based indexes, so that the first element in the array is element 0.

string[] days = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
System.Console.WriteLine(days[0]);  // Outputs "Sun"

Multidimensional Arrays

Conceptually, a multidimensional array with two dimensions resembles a grid. A multidimensional array with three dimensions resembles a cube.

// declare multidimension array (two dimensions) 
int[,] array2D = new int[2,3];

// declare and initialize multidimension array 
int[,] array2D2 = { {1, 2, 3}, {4, 5, 6} };

// write elements in a multidimensional array 
for (int i=0; i<2; i+)
    for (int j=0; j<3; j+)
        array2D[i,j] = (i + 1) * (j + 1);

// read elements in a multidimensional array 
for (int i=0; i<2; i+)
    for (int j=0; j<3; j+)

Jagged Arrays

A variation of the multidimensional array is the jagged array: an array of arrays. A jagged array is a single-dimensional array, and each element is itself an array. The element arrays are not required to all be of the same size.

You declare a jagged array like this:

int[][] jaggedArray = new int[3][];

Doing so creates an array of three arrays. These arrays can be initialized like this:

jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

Using the foreach Statement

The foreach statement is often used to access each element stored in an array:

int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
    System.Console.Write("{0} ", i);
//Output: 4 5 6 1 2 3 -2 -1 0

Arrays of Objects

Creating an array of objects, rather than an array of simple data types such as integers, is a two-part process. First you declare the array, and then you must create the objects that are stored in the array. This example creates a class that defines an audio CD. It then creates an array that stores 20 audio CDs.

namespace CDCollection
    // Define a CD type. 
    class CD
        private string album;
        private string artist;
        private int rating;

        public string Album
            get {return album;}
            set {album = value;} 
        public string Artist
            get {return artist;}
            set {artist = value;}
        public int Rating
            get {return rating;} 
            set {rating = value;} 

    class Program
        static void Main(string[] args)
            // Create the array to store the CDs.
            CD[] cdLibrary = new CD[20];

            // Populate the CD library with CD objects. 
            for (int i=0; i<20; i+)
                cdLibrary[i] = new CD();

            // Assign details to the first album.
            cdLibrary[0].Album = "See";
            cdLibrary[0].Artist = "The Sharp Band";
            cdLibrary[0].Rating = 10;


An array is just one of many options for storing sets of data by using C#. The option that you select depends on several factors, such as how you intend to manipulate or access the items. For example, a list is generally faster than an array if you must insert items at the beginning or in the middle of the collection. Other types of collection classes include map, tree, and stack; each one has its own advantages. For more information, see System.Collections, and System.Collections.Generic.

The following example shows how to use the List<T> class. Notice that unlike the Array class, items can be inserted into the middle of the list. This example restricts the items in the list so that they must be strings.

public class TestCollections
    public static void TestList()
        System.Collections.Generic.List<string> sandwich = new System.Collections.Generic.List<string>();


        sandwich.Insert(1, "lettuce");

        foreach (string ingredient in sandwich)
// Output: 
// bacon 
// lettuce 
// tomato

See Also


How to: Declare an Array

How to: Pass Object Arrays to Methods

How to: Iterate Through an Array

How to: Initialize an Array

How to: Iterate Through a Collection


C# Language Primer


Arrays (C# Programming Guide)

Collection Classes (C# Programming Guide)

Introduction to Generics (C# Programming Guide)