Please note that this article assumes you have read my previous tutorials on data types, variables, conditionals and loops, etc. and know how to properly compile a Hello, World! program.

Introduction

Arrays, which were mentioned briefly in Basic Concepts – Part 2, are found just about everywhere in code. In short, arrays are a simple way to store multiple values – of the same data type – in a single variable. By using arrays, you can avoid redundantly creating variables of the same data type.

Arrays (as mentioned previously) are zero-based in most programming languages. In other words, the indexes for the array will start at the number zero, as opposed to another number like one. In C#, arrays are objects just like everything else; they must be instantiated after declaring them, in order to use them in your program.

In this article, I will teach you its syntax and its usage in C#. Let’s get started!

Declaring and Instantiating

The following is the general syntax for declaring arrays:

For example, if you want to create two arrays – an array for storing int variables, and an array for storing string variables – you can do so as follows:

Note that they have only been declared at this point; they have not been instantiated. To instantiate them, you can do the following:

From this, you can see that the syntax for instantiating arrays is same as the syntax for instantiating any other object. Therefore it is fairly clear that arrays are “created” and treated just like other objects in C#.

Populating

For defining the actual elements in an array, you can use the following syntax:

As you can see, the first element in an array starts with the index number of zero. That is, if an array is of size ’10’ (new int[10]), then you can write and read elements from 0 to 9. Trying to talk to an index greater than 9 would throw an overflow exception, so keep this in mind when you use arrays in your program.

Note that in C#, there is no distinction between static and dynamic arrays; that is, the syntax for declaring them is same. For increasing the size of an array, you can simply instantiate them again as follows:

For retrieving the length of an array, you can use the property Length, which is available on every array.

But wait!

My Commodore 64 only has 29 bytes of free space after the C# libraries are loaded, and I want to be as efficient as possible! Is there a way I can both declare and instantiate an array on one line?

Yes! For those of you that, like me, don’t feel any need to migrate to a computer newer than a Commodore 64, declaring and instantiating an array at the same time is fairly easy. You can use the following syntax:

That will declare and populate an array, sizing it according to the number of elements present in your list.

Another interesting thing to be noted is that such loops can easily be used on arrays. For example:

Types of Arrays

As we have seen previously, we can declare single-dimensional arrays (the previously-discussed arrays) as follows:

In C#, there are other two types of arrays: multi-dimensional arrays and jagged arrays (also known as “arrays-of-arrays”). For declaring and creating multi-dimensional arrays, we can use the following syntax:

You can access the members of this array as follows:

For declaring and creating jagged arrays (or “arrays-of-arrays”), we can use the following syntax:

Now, you can access the members of this array as follows:

Use Cases

Arrays are commonly used in cases where you need to store the same type of data, but more specifically, large amounts of “the same type of data”. You can store the data in an array, instead of declaring each variable discretely. Later, you can retrieve them later simply from a single variable.

For instance, you may need to store the names of students. Without arrays, you would need to define a variable for every student’s name. With arrays, however, you can simply store all the students’ names in a single-dimensional array of type “string“, making the program easier to read and manage.

However, when we need a collection of different data types, then we use class or struct instead.

Just by looking through the code of a trivial (or complex!) program, you’ll see places where arrays could be used, pretty much every few lines. Its usefulness cannot be dismissed, considering the fact that it is implemented in every (or most) programming languages that we use. Have any question or comment about something I missed? Please let me know in the comment section below!

Share

Comments are closed.

Post Navigation