C# Arrays And Everything You Need To Know About Them

C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.


Andraz Krzisnik
C# Arrays And Everything You Need To Know...

C#, spoken Csharp, programming language offers us three different variations of arrays, which I’m going to describe in this post. But first of all, I’m going to talk about what arrays are even good for.

I’m sure that, as a developer, you’ve must have encountered a problem, where you needed to store a collection of data within your application. Even though there are many ways of doing that, using an array is probably the easiest and most natural one possible.

Arrays are data structures, which can hold many values of the same type. For example, you can create arrays of generic types like int, string or custom types you make yourselves.

Single Dimensional, Multi Dimensional And Jagged Arrays In C#

In this guide, I’m going to show you how to use different variations of arrays. Therefore, you might get to consider other options to solve your problem with better efficiency.

However, C# arrays of all varieties have one drawback in common. They’re all limited to have the number of items inside, which we specify when we initialize the array. This can cause problems, when we want to add data continuously into a collection.

Single Dimensional C# Array

This is the simplest form of an array, which we can use to store data in a single dimension. Furthermore, when we initialize the array, we only need to specify its size. Or in other words, how many items it’s going to hold.

//declare single dimensional array of integers
int[] numbers;

We can see how to declare a simple array of integers in the code section above. In order to use this array, we also need to initialize it.

//initialize single dimensional array of integers
int[] numbers = new int[3];

For example above, we created an empty array of integers, which can hold 3 integer values. Furthermore, if we initialize it without specifying what the values are, the values will default to 0.

We can change or call the numbers inside, by specifying their index number. Index numbers start from 0 and end at array size minus one. In the example I set above, first number will have the index of 0, second the index of 1 and last the index of 2.

We can define values when we initialize the array in a couple of different ways.

//define values inside array using indexes
numbers[0] = 4;
numbers[1] = 2;
numbers[2] = 6;

//initialize and define values at once
int[] numbers = new int[]{4, 2, 6};
int[] numbers = {4, 2, 6};

As you can see, if you initialize an array with already defined values it should store, you don’t need to define its size.

single dimensional c# array
Iterate through integer array and output each value inside it

Multi Dimensional C# Array

Multi dimensional array is similar to single dimensional array I described above. The only difference is that we need to specify its size in 2 or more dimensions. This will create an array, that can store values in multiple dimensions.

//declare and initialize empty multi dimensional array
int[,] multi = new int[2,3];

//declare and initialize multi dimensional array with predefined values
int[,] multi = new int[,] = 
{
   {3, 2, 1},
   {6, 4, 5}
};

Each of these values can be changed or called by specifying the index numbers for each dimension.

//access a number in the array
int number = multi[1,0]

The number in the example above will access number 6 from the preceding example array.

Jagged C# Arrays

Jagged arrays are basically arrays of arrays. In other words, they are single dimensional arrays, that hold arrays instead of values like integers or strings.

Their advantage to multi dimensional arrays is that the arrays, which it holds, can be of different length to one another.

However, in order to make them work, we need to initialize each array inside separately.

//declare and initialize jagged array
int[][] jagged = new int[2][]

//initialize also the arrays it stores
jagged[0] = new int[3]
jagged[1] = new int[2]

Example above shows how you can store arrays of different sizes inside the jagged array. You can also see jagged arrays at work in a Fourier Transform project for converting images into frequency domain.

Conclusion

Hopefully, this post helped you gain a better understanding of arrays and how to use them.

You can also download the example project I made, where you can see how it works in practice.

Related Articles

Morphological Processes

How To Make Opening And Closing – Morphology With C#

Opening and closing in image processing are morphological operations which are basically sequences of erosion and dilation operations.

Posted on by Andraz Krzisnik
C# Tutorial

How To Use Gaussian Lowpass Filter – C# Guide

We’ve talked about ideal lowpass filter and Butterworth lowpass filter already. And now, we shall get to know the last type of lowpass filters, which is Gaussian lowpass...

Posted on by Andraz Krzisnik