C# Arrays

Learn via video courses
Topics Covered

Overview

In the programming world, variables are used to store values in it. A single variable is used to store a specific type of value in it. However, if we want to store multiple values in a single variable, we use arrays. These arrays are linear data structures used to store multiple values of a specific data type in them.

In this article, we will be discussing C# arrays, how are they declared, and initialized, their types, the array length, and the different ways to create an array.

Introduction to C# Arrays

The C# Arrays are a group of similar types of values that are stored in contiguous locations in the memory which are referred to by a common variable name. These values stored in an array are also known as the elements of the array. Each element of an array can be accessed using a square bracket [] and an index that starts with 0. Therefore, the index a[0] refers to the first element of the array, a[1] to the second element, and so on, where a is the name of the array.

In C#, arrays are objects which have a base type of System.Array. The C# arrays can store both the primitive types as well as objects based on their definition. However, in the case of the primitive types, the array elements are stored at contiguous memory locations but in the case of objects of a class, the memory is allocated in the heap segment.

C# supports three different types of arrays which are as follows:

  • One-dimensional array:
    In this type of array, we have only one row for storing the values starting from 0 up to its length.
  • Multi-dimensional array:
    It is an array that has multiple rows to store the values of the array. This array is also known as a rectangular array as it contains the rows of same length throughout the array.
  • Jagged arrays:
    Jagged arrays are a special type of array also known to be an "array of arrays". These arrays also contain multiple rows to store the values but the dimensions of the different rows may be different.

Array Declaration

The syntax for declaring a C# array is as follows:

In the above syntax:

  • <Data_Type> refers to the data type of the values that are to be stored in the array.
  • <array_name> refers to the name of the array.

However, one thing to keep in mind is that declaring an array does not actually allocate memory to the array but initializing it does.

Let us see an example of declaring an integer array.

In the above example int refers to the type of the array elements followed by a [](square) bracket and arr being the name of the array.

Array Initialization

Array Initialization is the step in which the memory is actually allocated to the array. Array initialization is a way to initialize the size of the array and its respective elements. Initialization is done using the new keyword which creates a new instance of the class.

The syntax for initializing a c# array is as follows:

In the above syntax:

  • <Data_type> means the type of values that are to be stored in the array.
  • <array_name> refers to the name of the array.
  • new keyword is used to create an array and allocate space into the memory for the array.
  • <size_of_array> refers to the size or number of elements that are to be stored in the array.

Let us see an example for initializing an array.

In the above example, we have initialized an integer array of size 10 using the new keyword.

Note: The new keyword actually allocates the memory to the array based on its size.

Initialization of an Array after Declaration

It is not necessary that we have to initialize the array when we are declaring it, we could also do it after the declaration of the array. But in such a case, we necessarily have to use the new keyword to initialize the array without we cannot initialize it.

An example of this case is as follows:

Therefore, in the above example, we have first declared the array in the first line and then initialized it by using the new keyword.

Note: Initializing an array takes a time complexity of O(1).

Access the Elements of an Array

We could assign the values directly to the array while initializing it. However, we could also assign the values to the array elements after the declaration and initialization of the C# array using indexes.

These indexes start from 0 and go up to the length of the array. Since the array elements are assigned values through these indexes, therefore they could also be used to access them.

Therefore, to access an element from a C# array, we use the square brackets [] along with the index of that particular element that needs to be accessed.

Example 1

The code example for accessing an element from a C# array is as follows:

Output:

In the above example, we have first declared and initialized a C# array of size 3. After this, we assigned the values to the array using indexes. And then, accessed the particular value at the specific index from the C# array.

Example 2

Moreover, we could also access all the elements of the array using loops in C#. There are four types of loops popularly known as for, for-each, while, and do-while loops. Let us see the code example of accessing the array elements using all four loops used in C#.

Output:

In the above code example, we used loops to access all the elements of the array by going from 0 up to the length of the array and accessing the element at a particular index using array indexes.

Note: Time Complexity for accessing all the elements of an array using different loops is O(n) as we have to go to each element of the array and fetch it.

Change the Value of A Specific Element

There are situations when we have assigned a value to an element and then want to change that value. It could be easily done in C# by simply assigning the new value to that particular index that needs to be changed.

Example 3

Let us understand it with the help of a code example in C#.

Output:

In the above example, we have defined an array and changed the value of the particular array index 1 from 50 to 100.

Array Length

The C# arrays have a lot of properties that can be used to perform different operations on them. One such property is the Length which is used to find the number of elements in the array.

Example 4

Let us understand the Length property of C# arrays with the help of a code example.

Output:

In the above example, we have defined an array and then used the Length property on that array to find the number of elements in that array.

Different Ways to Create an Array

C# uses the new keyword to create an array by specifying its size. However, there are other ways as well in which a user can create an array in C#.

The different ways to create an array in C# are as follows:

  • Creating the array using the new keyword -
    In this method, we create an array using the new keyword with a specific size and initialize its value later in the program. An example of this is as follows:
    In this example, we have created an array of 100 elements with the help of the new keyword.
  • Creating the array using the new keyword and initializing the values as well -
    In this method, we create the array with a specific size and initialize its values right away along with the creation. An example of this is as follows:

In this example, we have created an array of a size 4 and simultaneously assigned the elements as well.

  • Creating an array without specifying the size of the array -
    In this method, we create an array using the new keyword, but without specifying its size. However, the elements of the array are assigned along with the creation. An example of such a case is as follows:
    As you can see in the above example, we have created an array arr and directly assigned the values to the array without specifying its size.
  • Creating an array without specifying the size and also without using the new keyword -
    In this method, we create an array without using the new keyword and directly assign the values to the array. An example of such a case is as follows:
    As you can see in the above example, we have first declared the array and then assign the values to it without the new keyword and the size of the array.

Therefore, we have discussed three different ways in which we could create a C# array. Any of these methods could be used by the user as per their convenience.

Note: In C#, Initializing without specifying the size of the array is acceptable when you declare and initialize an array together. However, if the declaration is done first and then initialization of the array is done at a later stage of the program, then the size of the array must be specified for initializing or creation of the array. Otherwise, the compiler would throw a compile time error.

Conclusion

  • A C# array is a collection of similar types of values stored in a common variable.
  • Arrays are declared accessed through indexes where 0 corresponds to the first index, 1 to the second index and so on.
  • A jagged array is an "array of arrays" and could contain rows of any dimensions.
  • Declaration of an array does not actually allocate any space into the memory for the array.
  • However, the initialization of an array is the step that actually allocates the space into the memory for the array.
  • We could easily change the array values through their indexes.
  • C# arrays have a property Length which is used to count the number of elements in the array.
  • There are different ways in which an array could be created, using the new keyword, without using the new keyword, specifying the size of the array, or even without using the size of the array.
  • Initializing an array at a later stage of the program must be done using the new keyword.