C# array Tutorial

When we want to store a range of values in one variable, we can use arrays.

For example let’s say we have 10 integer elements and we want to store them.

Instead of creating 10 different variables and store these elements in each variable, we can create an array of type integer and store all the elements in that variable.

The way we access these values is a little bit different than the usual way of accessing values in simple variables but before we get into those details, first let’s see the structure of an array:

Data-type [] array-name;
`data-type`: here we set the data type of the values that are going to be stored in the array.

Note: all the values should be of the same data type.

`array-name`: variables have names no-matter if those variables are simple or compound like this one here. So the `array-name` is basically the name of the array.

Note: all the rules that apply to name of a variable, also apply to the name of an array variable.

`[]`: We need to use the pair of brackets between the data-type and the array-name and this signals the compiler that the variable is actually an array variable.

So far we’ve just declared a variable as an array. But now if we want to store values in that array, we have two options:

  1. Direct initialization: to directly initialize
    the array with values right where the declaration is done, we can use
    the assignment `=` operator and after that via a pair of braces `{}` we
    can store the values in the array.

Example:

int []iArray = {1,2,3,4,5,6};

Here the name of the array is iArray and it stored 6 elements.

Notes:

  • In the braces we use comma `,` to separate values from each
    other.

  • The size of the array in this case is automatically declared and
    it is equal to the number of elements we set for the array. For example
    if we initialized an array with 6 elements, then that array has the
    length of 6.

  • The size of an array is static and that means after we or the
    compiler declared the size of that array, we can’t change the size and
    store more values in that array anymore.

  1. Allocate the size and initialize it later:
    Sometimes we don’t have all the values right away! So in such case we
    can create the array, set its size and then later in the program store
    the elements when they are cleared.

This is how we set the size of an array:

Data-type [] array-name = new Data-type [number-of-elements];

As you can see, after the array-name we use the assignment = operator and after that comes the new keyword; followed by that comes the name of the data-type which should be the same as the data-type of the array-name and finally the brackets.

number-of-elements: This is where we declare the number of elements that we want this array to be able to store. For example if we want the target array to have enough memory space for storing 100 elements, then we set the value 100 as the input in the pair of brackets.

Example:

int []iArray = new int[100];

Here the type of the array is int and it can store 100 elements.

Arrays have index number and it starts from 0. We use the index number to reach the target element in the array in order to retrieve or assign a value in that index.

This is how we use index numbers in arrays:

Array-name[index-number]

`array-name`: this is the name of the target array.
`[]`: after the name of an array, comes the brackets and within the brackets we set the index number.

Note: because the first item in an array is at index 0, the final item in that array will be at index number-of-elements -1.

For example let’s say an array has 10 items; the first element in that array is at index 0 and the final element will be at index number 9 which in total we will have 10 elements.

Example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
int []age = new int[10];
age[0] = 10;
age[1] = 20;
age[2] = 30;
age[3] = 40;
age[4] = 50;
age[5] = 60;
age[6] = 70;
age[7] = 80;
age[8] = 90;
age[9] = 100;
Console.WriteLine("The value at index 4 is: " + age[4]);
}
}
}

Output:

The value at index 4 is: 50

As you can see, in order to assign a value to an element of the age array, we put the array’s name and brackets on the left side of the assignment = operator and the target value on the right side. This will replace the value that is already in that index (if there’s one already) with the new value.

Also calling the array with brackets and index number in other places (other than being used as LValues (on the left side of the assignment operator)) will return the value that is stored in the target index. This is what we did when called age[4] in the WriteLine() method.

Getting the length of an array:

In later sections we will introduce what objects are, but for now just remember that when we create an array, we’re actually creating an object.

This object has different methods and properties that can be used.

One useful property of an array is the Length property.

Via this property we can get the number of elements that can be stored in a particular array.

Example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
int []age = new int[10];
for (int i = 0; i < age.Length; i++)
{
age[i] = i;
}

foreach (int i in age)

{
Console.WriteLine(i + ", ");
}
}
}
}

Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

To use the Length property, we put dot . after the name of that array like age.Length and this will return the length of the array.

In this example we’ve created a for loop to go through all the index of an array and store a value in each of those indices.

After that via foreach loop, we went through the indices of the array and printed all the elements to the screen.

Constant arrays:

An array can be constant as well.

To turn an array into constant we put the keyword readonly behind the data-type of that array when declaring it. In the readonly section we’ll explain this keyword in greater details but for now just remember that when adding the readonly to an array, you’re no-longer allowed to assign another array to that variable. But remember you still can change the elements of the current array as you wish.

Example:

using System;

namespace quickExample

{
public class Program
{
static readonly int[] range = { 1, 2, 3, 4, 5, 6, 4 };
public static void Main(string[] args)
{
int[] array = new int[30];
range[0] = 10;
range[2] = 100;
range[0] = 1000;

//Error because we can’t assign another array to a readonly array-variable.

range = array;
}
}
}