C# Arrays

Array is a data structure. Array is used to store collection of data. We can store the integers, strings, decimal values in an array. It stores the values in continuous memory location.

An array has fixed size in nature. By using index we can access the array elements. The lowest index indicates the first element of the array and the highest index denotes the last element in the array. We can access array elements directly by using its index.

An array index always start from 0. Below is the diagrammatic representation of array elements how it is store in memory and how can we access them by using index.

Zero Based Indexing

Array Declaration

Arrays are reference types. It is the first step when comes to array. It is very important, how we declaring the array.

If it is an integer array, we can declare that array like below.

For string array, we want to declare like above.

Array Initialization

Since arrays are reference types we must have to initialize the array. Otherwise by default it would be initialized with null. Array declaration alone is not enough, we need to initialize the array

Whenever we initialize the integer array, by default the array elements are initialized with 0.

We can give array declaration and initialization in the same line also. Overall it would be like below,

Integer array of length 5
String array of length 5
Declaring and initializing with integer values.
Declaring and initializing with string values

Below example to get the clear the understanding of an array, how to initialize and how to retrieve the array elements by using index

We can also modify the array elements by using its index, like below

Make sure that, while modifying or accessing array elements, your index value must be smaller than arrays total length value which you have initialized. Otherwise you will get a System.IndexOutOfRangeException.

Enumerating Array values by foreach loop

It enumerates the elements and return the elements in index order. Below is the classic example of foreach loop

Foreach Loop example

In C#, arrays are implemented in .NET execution engine.

How arrays are stored in memory?

Consider the below example, An array of 4 integers. In C#, int occupies 4 bytes in memory. So that,an array of 4 integers can occupy 4×4 = 16 bytes in memory.

Array elements are stored sequentially in memory.

Why accessing the array elements is very fast and efficient?

The reason is index. If you know the index of the element you want, then it is extremely easy to fetch the element from the memory. Because, we know the exact address(index) of the element.

An array is said to be Index based list, because the elements are stored sequentially in memory.

Why array is fixed size in nature?

When you initialize an array with size, it occupies those size in memory, it will not extend the memory whenever you add a new elements. For example, when you initialize an array with size 5, it occupies 5 contiguous locations in memory, it will not add extra memory in it.

How integer array is by default initialized with 0 values?

In C#, compiler takes the responsibility over this. Whenever compiler sees the array in code, it first initialize them with default values as 0 in very fast. Mostly in other areas constructor will do this job, but in C#, compiler has the full responsibility over arrays.

Leave a Comment

Your email address will not be published. Required fields are marked *