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

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

Leave a Reply