Website development and design blog, tutorials and inspiration

Using Arrays and Lists in C#

A look at arrays for storing lists of data

By , 9th November 2007 in C#

Arrays are used to store collections of data in a numerical indexed list. Arrays can be one-dimensional, multi-dimensional or jagged.

To start with we will look at a simple one dimensional array of integers.

An array is declared using brackets after the data type, with an element count in the brackets. Arrays in C# are of a fixed length, not dynamic. We will cover dynamic arrays (ArrayList and List) later in this tutorial.

  1. int[] myIntArray = new int[10] // Create an array of 10 ints

Note: Although arrays are zero indexed, which means that the first item in the array is 0, the second is 1 and so on, when declaring arrays you specify the total number of elements.

Arrays can be assigned to individually or in a loop:

  1. int[] myIntArray = new int[10];
  2. myIntArray[0] = 0;
  3. myIntArray[1] = 10;
  4. myIntArray[2] = 20;
  5. myIntArray[3] = 30;
  7. // Assignment via loop
  8. for (int i=0; i<myIntArray.Length; i++)
  9. {
  10. myIntArray[i] = i * 10;
  11. }
  13. // Foreach loop over array
  14. foreach (int element in myIntArray)
  15. {
  16. Console.WriteLine("{0}", element);
  17. }

Arrays can also be assigned to on declaration instead of specifying a length.

  1. int[] myIntArray = {0,1,2,3,4,5,6,7};

Multi-dimensional Arrays

A multi-dimensional array consists of two or more elements that form a matrix. Jagged arrays are multi-dimensional arrays of arrays and covered later.

  1. int myMultiArray = new int[5,5];
  2. int myOtherMultiArray = int[5,2];

Assignment and access is the same as a single dimensional array, but you need to specify both elements.

  1. for (int y=0; y<=5; y++)
  2. {
  3. for (int x=0; x<=5; x++)
  4. {
  5. myMultiArray[x,y] = x * y;
  6. }
  7. }

Jagged Arrays

Jagged arrays, or array of arrays, are multi-dimensional arrays that consist of arrays as the elements.

  1. int[][] myJaggedArray = new int[2][];
  3. myJaggedArray[0] = new int[5];
  4. myJaggedArray[1] = new int[] {0, 1, 2, 3, 4, 5, 6, 7}

Array Methods

All arrays are objects that inherit methods and properties from the base Array class. These can be used to determine the number of items in an array (Length) and the number of dimensions (Rank).

Array Lengths

Once an array is declared with a number of elements, its length cannot be changed. If you need more than the number of elements declared you will have to create another array. You cannot re-declare an array in C#.

  1. int[] myIntArray = new int[10];
  2. int[] myLargerIntArray = new int[20];
  4. myIntArray.CopyTo(myLargerIntArray, 0);

One solution to this problem is to use a List or an ArrayList, which are much more powerful than simple arrays.

ArrayList and List

Not available in .Net 1

An ArrayList is a class that holds values like an array, but elements can be added or removed at will (dynamic array). They offer greater functionality over an array, but they also have a larger overhead. ArrayLists are not type safe, meaning that each element of the ArrayList can be of a different type.

  1. using System.Collections.ArrayList;
  2. ArrayList myArrayList = new ArrayList();
  3. Exception ex = new Exception("My Demo Exception");
  5. myArrayList.Add("Hello");
  6. myArrayList.Add("World");
  7. myArrayList.Add(10);
  8. myArrayList.Add(ex);
  10. foreach (Object obj in myArrayList)
  11. {
  12. Console.WriteLine(obj.ToString());
  13. }
  15. /* Output:
  16. Hello
  17. World
  18. 10
  19. System.Exception: My Demo Exception
  20. /*

A List is a type safe version of an ArrayList.

  1. using System.Collections.Generic;
  3. List<string> myList = new List<string>();
  5. myList.Add("Hello");
  6. myList.Add("World");
  7. myList.Add(10); // Compiler Error

Note: the value in angle brackets is the type that the list will contain. Also note the parenthesis at the end of the declaration statement. Type can be any type of object defined in C# including classes.

Both ArrayList and List support searching and sorting methods.

  1. Anne-Marie Armour
    Anne-Marie Armour

    My C# Express will not accept ...
    int[] myIntArray = int[10];

    It insists that I use the "new" keyword ...
    int[] myIntArray = new int[10];

    And it insists that ...
    int myMultiArray = int[5,5];
    int myOtherMultiArray = int[5,2];
    becomes ...
    int[,] myMultiArray = new int[5,5];
    int[,] myOtherMultiArray = new int[5,2];

Leave a Reply

Your email address will not be published.