Arr… in C#

Sudhanshu Jain
3 min readDec 31, 2019

One of the most interesting topic of C# which you find most commonly asked in interview questions called ARRAYS. If you are C# lover you might find this blog interesting and productive.

Like any other programming language, arrays in C# is a group of homogeneous or similar types of elements that have contiguous memory location. Here also, the array starts from 0 index position and fixed sets of elements.

In C#, array is an object of base type System.Array.

Single Dimensional Array

In above diagram, the array is Single Dimensional.

  • The array containing homogeneous types of elements. Here it is integer type.
  • The array starts from 0 position and going up to 6th position therefore overall length of the array will be 7.
  • The size of the array as per C# language as 28 byte. Integer type is of 4 byte each block. (4 * 7 =28).

There are three types of arrays as in C# :
1. Single Dimensional Array.
2. Multi- Dimensional Array.
3. Jagged Array.

Single Dimensional Array can been declared and initialized at the same time as show in following example (Initialization is not Mandatory).

Ways of Declaring and Initializing the Arrays.

Multi Dimensional Array :

One of the topic which is asked numerous times in the interviews so be smart while answering the question related to that.

Most of the programmers think about memory in n dimensional manner which is leading to the confusion. Keep it simple !!

Here is the diagram, that how the two dimensional array adjusted in to the memory :

Multi Dimensional Array

You can also increase from 3 dimensional to the n dimensional as per your need. Multi Dimensional Array is also known as Matrix and it is easy to represent in the Matrix Format.

Matrix Representation

The above matrix example is of size [3,3].

Let’s take a look of three Dimensional array, it sometimes feel difficult but I will give you the idea of thinking for three dimensional array. You can start by observing the speedometer of vehicle.

The last value of speedometer will always start as 0 and then once it reaches to max level as 9, it will shift the next left value from 0 to 1 and this process continues.

Let’s do an exercise of size [3,3,3].
How many possible cases will be there ?

Let’s count together,
000, 001, 010, 011, 100,101,110,111

Here is the Practical Code :

Two and Three Dimensional Arrays

Jagged Arrays

In C#, Jagged array is also known as “array of arrays” because its elements are arrays. The element size of jagged array can be heterogeneous
(Not similar size).

Declaration and Initialization of Jagged Arrays

Jagged Arrays Declaration and Initialization c#

That’s all for now. If you like this hit clap as much as you want (NO RESTRICTION).

--

--

Sudhanshu Jain
Sudhanshu Jain

Written by Sudhanshu Jain

Senior Software Engineer | Scrum Master Certified | Blogger | Tech Mentor

No responses yet