[“C#”, “Arrays, “Lesson”][“part”, “2”, “multi-dimensional arrays”]

Advertisements

Hello everyone,

Michael here, and in a continuation of our previous lesson, today we’ll explore more about C# arrays-this time with a focus on multi-dimensional arrays.

[“basics”, “of”][“multi-dimensional”, “arrays”]

So, what is a multi-dimensional array in the context of C#? To put it simply, a C# multi-dimensional array is two or more 1-dimensional arrays put together. Here’s an example of one:

int[,] years = { { 2018, 2020, 2022, 2024 }, { 2019, 2021, 2023, 2025 } };
Console.WriteLine(years[0, 2]);

2022

This is a simple, 2-dimensional array in C#. It’s conceptually similar to 2-dimensional (or multi-dimensional arrays for that matter) in languages like Java or Python, but there are things worth noting for multi-dimensional arrays in C#:

  • To initialize a multi-dimensional array in C#, use the following syntax: data type[,] name of array.
  • The , in between the square brackets indicates the number of dimensions the array will have. For instance, one comma indicates a 2-dimensional array, two commas indicate a 3-dimensional array, and so on.
  • Each individual array-with-the-array needs to use curly brackets {} instead of square brackets []. It’s just one of those C# things.
  • Just like with 1-dimensional C# arrays, multi-dimensional C# arrays use a 0-based indexing system. However, to access an element of a multi-dimensional C# array, this is the correct syntax-years[0,2]-not this-years[0][2].

[“modifying][“multi-dimensional arrays”]

Now, what if we want to modify an element in a multi-dimensional C# array? Here’s how we can do so:

years[1, 3] = 2027;
Console.WriteLine(years[1, 3]);

2027

In this example, I’m changing the fourth element in the second dimension of this array from 2025 to 2027. Just as I did when I changed elements in the 1-dimensional array, I referenced the index of the element that I wanted to change and assigned the new value I wanted to use to that index.

[“other”, “questions”][“about”, “multi-dimensional arrays”]

Now, you’ll likely have other questions about multi-dimensional arrays in C#. First of all, can you sort them like you can with 1-dimensional C# arrays. The short answer to that-no:

Console.WriteLine();
Array.Sort(years);
foreach (int y in years)
{
    Console.WriteLine(y);
}

As you can see from the error message, you can only sort single-dimensional arrays in C#. Presumably, that means you can’t reverse-sort C# arrays right? Correct!

Console.WriteLine();
Array.Reverse(years);
foreach (int y in years)
{
    Console.WriteLine(y);
}

Now, are we able to at least find the length of a multi-dimensional C# array? Yep!

Console.WriteLine();

Console.WriteLine(years.Length);

8

Granted, the Length method will return the total number of elements in a multi-dimensional C# array (8 in this case) as opposed to say, something like (2, 4)-which would imply a 2×4 array.

Here’s the code on the GitHub-https://github.com/mfletcher2021/blogcode/blob/main/MultiDimensionalArrays.cs (the error-yielding Sort/Reverse code has been removed, but if you wish to test it, feel free to plug it back in anywhere on the code)

Thanks for reading,

Michael

[“C#”, “Arrays”, “Lesson”, “part”, “1”]

Advertisements

Hello everybody,

Michael here, and this post will be a part 1 of my C# array exploration, covering 1-dimensional arrays!

You could say this array moves in 1, 2 step.

What are arrays? Well, you’ve likely seen me cover them in Java and Python-arrays basically store lists of values in your code. The values could be of any type-strings, integers, etc.

For a refresher, here’s what arrays look like in Python:

pythonArray = ['test1', 'test2', 'test3', 'test4', 'test5']

And here’s what arrays look like in Java:

String[] javaArray = {'test1', 'test2', 'test3', 'test4', 'test5'};

[“C#”, “1-dimensional”, “arrays”]

Now, the first thing I will show you how to do is make a simple 1-dimensional array in C#. Take a look at this example:

string[] languages = ["Python", "R", "Java", "MySQL", "CSS", "Bootstrap", "HTML", "C#", "GitHub"];
Console.WriteLine(languages[2]);

Java

How does this simple C# array work? Here’s what you should keep in mind:

  • C# arrays are created like Java arrays; both types of arrays are created using this structure-value type[] arrayname = [elements];.
  • If there’s anything C#, Java, and Python arrays have in common, it’s the 0-indexing system (as in, the first element of an array is always index-0).

In this example, I created an array that stores all the programming languages I’ve covered on this blog and accessed the third element of that array by using languages[2]. In this example, the third element of my languages array is Java.

[“C#”, “arrays”, “sorted”]

Now that we’ve explored basic C# arrays, how can we sort them alphabetically? Take a look at this example:

Array.Sort(languages);
Console.WriteLine();

foreach (string l in languages)
    {
    Console.WriteLine(l);
}

Bootstrap
C#
CSS
GitHub
HTML
Java
MySQL
Python
R

Now, how did I manage to sort the languages array? C# has a method for array sorting aptly called Array.Sort() which allows me to sort the languages array alphabetically.

As for the foreach loop, consider this a preview for my eventual C# loops lesson (and don’t worry-I’ll explain the idea of loops in greater detail). If you understand the idea of loops from other programming tools, you could probably guess that the foreach loop here is basically iterating though the sorted languages array and printing out each element one by one in alphabetical order.

Now, what if we wanted to sort this array the other way around-i.e. in reverse alphabetical order? Here’s how we’d do so:

Console.WriteLine();
Array.Reverse(languages);

foreach (string l in languages)
{
    Console.WriteLine(l);
}

R
Python
MySQL
Java
HTML
GitHub
CSS
C#
Bootstrap

It’s literally like what we did before-the only difference is that you’d use the Array.Reverse() method to sort the array in reverse order. And just like that, we see all the elements in the languages array printed out one by one in reverse alphabetical order.

  • Also, no need to worry about the Console.WriteLine() lines in this code, as I just used them to space out the output between examples since I’m using one script for each example.

[“C#”, “modifying”, “arrays”]

Last but not least, let’s see how we can modify arrays. Let’s change an element inside the array:

Console.WriteLine();
languages[2] = "JavaScript";
Console.WriteLine(languages[2]);

JavaScript

In this simple example, I changed the third element (index-2) of the array from Java to JavaScript. Pretty simple right?

Now, you may be wondering if we can add or remove elements inside a C# array. The simple answer to that question is sadly, no, because unlike arrays in Python (and perhaps other languages too), C# arrays have a fixed size, which means you cannot add or remove elements from an array once it’s been declared in C#.

[“C#”, “array”].length()

Last but not least, let’s see how long our C# array is:

Console.WriteLine();
Console.WriteLine(languages.Length);

9

By simply using the .Length method, you can see how long your C# array is. In this case, the languages array is of length 9.

As always, here’s the script that I used for this lesson-https://github.com/mfletcher2021/blogcode/blob/main/1darray.cs.

Thanks for reading,

Michael