I have been coding in C# on and off since the end of 2013. You might be wondering why I mentioned that here though.

Confused Liam Neeson

The reason is that, a large part of that time especially between 2015 to 2021, I was pretty much managing a team, rather than coding full-time. Now, does that make a difference? Of course, it does. Being primarily in a managerial role meant that I had less time to code and more time to do non coding tasks.

What are these non coding tasks?

I did spend quite a lot of time designing and discussing solutions with my direct reports. This was probably the fun bit. I also did a lot of code reviews and when I say code reviews, I don’t just mean reviewing one programming language. I reviewed code written in Perl, CSharp, JavaScript and bash. That means I have to know a little bit off all those things I mentioned. This made me or actually makes me a generalist and not really a specialist in any one programming language. In popular terms, I would be the jack of all trades and master of none. There isn’t a day that I don’t wonder, why I chose to be this way. Well, I think that’s life. You plan something then something completely different happens. Then as a manager, I also had to do regular catch-ups with my direct reports, annual performance review, setting annual goals, attending quarterly target meetings, conducting interviews etc.

Anyway I think I digress.

Homer simpson episode 22 GIF

The title of this post might actually seem really odd. After all, every programmer knows about arrays in CSharp. Why would one have to write a blog post about it? Well, you are partly right, if you are a seasoned C# programmer. Arrays are like one of the most fundamental data structures in programming and hence, you wouldn’t think that someone would have to write a blog post on it. Well, there are multiple reasons for this post.

Schitts Creek What GIF by CBC

Having worked with someone from a non-computer science background, I learned the importance of explaining programming concepts in simple words. And I think, I will try to do this with every post here.

The second reason is personl.

In the past few weeks, I’ve been trying to teach myself a bit more about nodejs and because of this I have been coding a lot in javascript over my free time, like weekends. Not building huge projects, but playing with little problems. Then suddenly, one day, I decided to do some programming problems on LeetCode. And because my primary programming language at work is C#, I decided to write a solution in CSharp. Despite writing code that looked like it would work, I got compilation errors!

Kitty Quit GIF

This caught me off guard. I wasn’t prepared for it because the problem, was in my standards, very simple. So why could I not get it right?

Matrix GIF by memecandy

The problem I’m talking about is one that involves finding the transpose of a matrix. This might be a familiar one if you are into programming. However, if you are not, then I can try to explain it.

A Matrix is a tabular representation of values. It has a certain number of rows and a certain number of columns. Matrices are something I learnt in school mathematics. I think it was first introduced when I was in 10th grade or maybe 12th or earlier; I’m not entirely sure. So if there was a matrix of numbers, it would look like a table.

A matrix of numbers of 3 rows and 3 columns

In fact some of the most popular programming questions are actually around doing things with matrices. This could be because matrices in its simplest form, is generally represented using a multidimensional array. Well, if you are not a programmer, that might sound like I’ve just vomited a lot of words.

Arrays?

In computer science, if someone said arrays, it means, they are referring to a data structure of that name. It consists of a collection of elements, each of which is stored at unique contiguous positions in memory. And because they are stored in contiguous locations in memory, if you have an idea where the first element is, then you can easily go through the entire list or pick a particular value in the array, but just knowing its position in the array.

C# Arrays

An example representation of array in memory - https://www.geeksforgeeks.org/c-sharp-arrays/

The values in the array, which in the picture above are: 7, 11, 6 and so on, can be accessed by using the index.

So if I wanted to access 55 in the arrayk, I could just ask the computer to give me, give me the element at index 3.

You might have noticed how, despite 55 being the 4th element in the array, is referred to by index = 3. This is because, in **_most _**programming languages, array indices start from 0. There are many programming languages that do not follow this convention though. And the reason this differs comes down to the underlying programming language design.

So Arrays are collection of values, so what’s quirky?

In the earlier example, I use the picture of a single dimensional array. What if I wanted to represent a matrix using arrays?

Multidimensional arrays

It is fairly simple to visualise a multi-dimensional array, which is why I brought up the matrix earlier. It is a set of arrays stacked on top of one another.

many types of arrays

Arrays in C#

The C# programming language has multiple types of arrays. This is not common, but it does have it.

Multidimensional or Rectangular arrays

The terms in the title of the section refer to the same thing in C#. This is a special datastructure that can store more than one row to store values. However, every row is not a dimension. This is why the example image of the different types of arrays, is useful. This type of an array is called rectangular because, the length of every row in the array is the same, however you look at it.

   
    // syntax of declaring a multidimensional array is unique in C#
    // 2d array above is delcared in c# as follows
    int[,] intarray = new int[3, 2];
    
    // a 3d array like the one in the picture above, in c# is declared as follows:
    // creates an array of three dimensions, 3, 3 and 2
    int[,,] intarray1 = new int[3, 3, 2];

Jagged Arrays

This is the less popular version of the array. The name has a clue. Unlike the earlier type, every row, or every array in the stacked set of arrays, could have a different length, not that it should, but can. So you could use a jagged array to represent a rectangular array too. No one would stop you but you might get questioned when it comes time for a code review.

The declaration of this one in C# looks very different from the earlier one. But would be familiar to you already if you are coming from another programming language.

    // syntax of declaring a multidimensional array is unique in C#
    // 2d array above is delcared in c# as follows
    // notice how there are two sets of square brackets
    // the second bracket is empty! does not mention a fixed size!
    int[][] arr = new int[3][];
    
     // Initialize the elements: 
     arr[0] = new int[5] { 1, 2, 3, 4, 5}; 
     arr[1] = new int[4] { 1, 2, 3, 4 }; 
     arr[2] = new int[6] { 1, 2, 3, 4, 5, 6}; 
    // every array has a different size!
    
    // a 3d array like the one in the picture above, in c# is declared as follows:
    // creates an array of three dimensions: 1, 1 and 2
                int[][][] a = new int[1][][];
                a[0] = new int[1][];
                a[0][0] = new int[2] { 0, 1 };
                for(int i = 0; i < 1; i++)
                {
                    for(int j = 0; j < 1; j++)
                    {
                        for (int k = 0; k < 2; k++)
                        {
                            Console.WriteLine($"a[{i}][{j}][{k}]: {a[i][j][k]}");
                        }
                    }
                }

In a jagged array, what you have to remember is that calling a.Length could give you a value that is different from a[1].Length and so on.

So that is already weird but wait till you see more.

Wait For It GIF by ZDF

Jagged Multi-dimensional array

Wait a minute?

Come On What GIF by MOODMAN

Well, I have never done this in a real life project, but C# lets you do this. You could, for the purpose of your heart’s content, create a jagged array that stores a certain number of multi-dimensional arrays!

    // Declaration and Initialization of  
        // Jagged array with 2 2-D arrays 
        int[][, ] jagged_arr1 = new int[2][, ] {
                                        new int[, ] {{1, 2}, {3, 4}}, 
                                        new int[, ] {{0, 2}, {4, 6}, {8, 10}}, 
                                        }; 

I don’t even have words to explain this. But what those lines would do is create a jagged array, that holds two multi-dimensional arrays of varying lengths. The varying lengths is possible, purely because, this was a jagged array in the first place! I personally haven’t found a use for this primarily because, I haven’t been coding forever in C#. Although I still wonder when I would want to use a jagged multi-dimensional array to represent something in my program. However, a simple jagged array, could be used to store a lot of different real world things.

Anyways, I guess I have successfully confused you enough. So stopping for now.