Sunday, July 14, 2024 11:21

Table of contents >> Arrays > Arrays


You’ve made it this far! Well done! Starting with arrays, we can already say we have a very basic knowledge of C# programming. Still a long path ahead of us, but we are no longer n00bs. We now know the fundamental things of (almost) any programming language and we can write basic programs. Not very useful yet, true, but hang in in there!

So, what are these arrays? Do they move? Do they bite?

No, not quite. The most basic definition of an array is “a sequence of elements of the same kind”. Well, it wasn’t that bad, was it? We dealt with this before, almost daily!

So, lets start with the beginning. We said arrays are sequence of elements of the same kind. To better visualize this, look at the following image:


In this case we have an array of 5 elements, which can be identified by an indexwhich starts from 0 (always remember! arrays are 0 based indexed, their elements start from index 0, not 1!!!) and goes all the way up to 4. You can imagine this array as a building (the array itself), with 5 apartments (the array’s elements) identified by an apartment number (the index).

All array elements must be of the same type. We cannot declare an array that contains an integer as the first element and a string as the second. If we declare an array of the type int, it can only store integer values, and so on.

There are two types of arrays: one-dimensional arrays (also called vectors) and multi-dimensional arrays (of which the most used ones are two-dimensional arrays, also known as matrices).

So, how does an array look like, in the end? To your surprise, not very different than what we previously learned when we dealt with primitive data types. The following example demonstrates the declaration of an array:

That’s it! By simply placing two square brackets after our variable type, we told the compiler to create an array of that type. In our case, we created an array of type integer, which, as we previously stated, can only hold the same type of elements – integers.

In our future lessons, we will deal at large with declaration, initialization and usage of arrays.

The concepts explained in this lesson are also shown visually as part of the following video:


Leave a Reply

Follow the white rabbit