Programming Basics: Arrays
A brief overview of the Array object in C#.
Arrays. Every programming language has their version. C# is no different. What exactly is an Array? In simplest terms, an array is a data structure that allows you to store a group of variables. You can use any data type for the variables, as long as all elements inside the array are of the same type.
“Okay”, you’re thinking, “but why are they so special?” You ask such intelligent questions! Arrays are special for one main reason — elements can be looked up with extreme efficiency. This is because arrays use indexing to give order to the elements in the array. Think of it like any alphabetical list. As long as you know the first letter of the word or item name you’re looking for, you can quickly and easily make your way to the needed information. However, if you only know the second letter of the word, you might as well have to read each word on the list in order to find the tricksy item.
Now, back to that indexing. As we have now learned, this is the key and the biggest advantage of arrays. Indexing provides ways for us to access various methods of retrieving information from the array with expediency. However, there is one slight caveat that you need to keep in mind when working with arrays. Computers count differently from (most) humans. We like to start counting from the number one. Computers on the other hand, begin counting from zero.
Why does this make a difference? Think about it, if you want the first item, and you ask the nice Mr. Array to look at item number one, it’s going to give you what you ask for — only it won’t be the first item in the array, it will be the second. Additionally, if you want the last item, and you know that the array has 5 elements, your natural instinct will be to ask for item number five. Do you see the issue? As far as the computer is concerned, there is no number five, as an array with five elements is only indexed up to the number four: array[0, 1, 2, 3, 4]
. Forgive me for belaboring the point, but some of the most common errors in programs are indexing errors. It is very easy to miscalculate and have a one-off error. That’s why its imperative to keep zero indexing in mind.
C# specifically allows for three main types of arrays: Single-dimensional, jagged, and multidimensional. Single dimensional arrays are the most straightforward, as they are an array with primitive data type. A jagged array is an array of arrays. Yep, you can have an array with array data types as elements.
// Single-dimensional
// Declare data type - Give name - set to a new int array of 8 itemsint[] mySingleDimensionArray = new int[8];// Jagged Array
// Declare a single-dimensional int array with int array data
// Data must be initialized prior to using
// We will initialize the data at declarationint[][] myJaggedArray = new int[][] {
new int[] {4, 8, 10, 12},
new int[] {245, 902},
new int[] {0, 9, 5, 25},
new int[] {77, 83, 76, 80},
new int[] {1, 5, 10, 20, 50, 100}
};
Lastly, we have multidimensional arrays. These can be a bit complicated, but think of it like a table with rows and columns. The first row contains a certain number of arrays acting as columns, then each array stacks below the previous row.
// Declare a 2D array with data of type string and initialize valuesstring[,] my2DArray = new string[3, 2] { { "bird", "fish" },
{ "dog", "cat" }, { "hamster", "iguana" } };
// Declare a 3D array with dimensions specified and data initializedint[,,] my3DArray = new int[4, 3, 4]
{ { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } },
{ { 16, 17, 18, 19}, { 20, 21, 22, 23}, { 24, 25, 26, 27}, { 28, 29, 30, 31} },
{ { 32, 33, 34, 35}, { 36, 37, 38, 39}, { 40, 41, 42, 43}, { 44, 45, 46, 47} } };
If you’re asking yourself why the 3D array only goes up to 47, read paragraphs 3 and 4 above for the answer 🙃.
I hope this C# array primer was helpful! Feel free to reach out with any questions. Until next time… Happy Coding!