Web Design that taps into the haromny and vision of your dreams.

Using Arrays and Lists in C#

A look at arrays for storing lists of data

Written By on in Coding

565 words, estimated reading time 3 minutes.

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

C# Programming Series
  1. Introduction to Programming
  2. What is C#?
  3. Your First Console Application in C#
  4. Introducing Methods and the Main() Function in C#
  5. Introducing C# Classes and Structs
  6. C# Data Types, Variables and Casting
  7. C# Program Flow Control and Entry Points
  8. Passing Parameters to Methods and Return Values in C#
  9. C# Access Modifiers and Scope
  10. C# Interfaces and Classes
  11. Using Namespaces in C#
  12. C# Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. C# Constants and Read-Only Variables
  16. Error and Exception Handling in C#
  17. Using Recursion in C#
  18. C# Operator List
  19. Class Inheritance in C#
  20. C# Class and Method Attributes Explained
  21. C# Class Constructors and Destructors
  22. C# Generics Variables
  23. XML Serialization and Deserialization
  24. C# String Formatting Examples

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.

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:

int[] myIntArray = new int[10];
myIntArray[0] = 0;
myIntArray[1] = 10;
myIntArray[2] = 20;
myIntArray[3] = 30;
// Assignment via loop
for (int i=0; i<myIntArray.Length; i++)
  myIntArray[i] = i * 10;
// Foreach loop over array
foreach (int element in myIntArray)
  Console.WriteLine("{0}", element);

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

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.

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

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

for (int y=0; y<=5; y++)
  for (int x=0; x<=5; x++)
    myMultiArray[x,y] = x * y;

Jagged Arrays

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

int[][] myJaggedArray = new int[2][];
myJaggedArray[0] = new int[5];
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#.

int[] myIntArray = new int[10];
int[] myLargerIntArray = new int[20];
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.

using System.Collections.ArrayList;
ArrayList myArrayList = new ArrayList();
Exception ex = new Exception("My Demo Exception");
foreach (Object obj in myArrayList)
/* Output:
System.Exception: My Demo Exception

A List is a type safe version of an ArrayList.

using System.Collections.Generic;
List<string> myList = new List<string>();
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.

Last updated on: Friday 23rd June 2017


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];

Reply to Anne-Marie Armour


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.


Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me