Arrays in C#

Introduction to Arrays

Arrays are a way to work with sequences of items of the same type.

array-msahebhonar.com

In C#, an array’s items are numbered from 0 to n-1. Those numbers are called indices. We can access an individual array items for reading and writing using the name of the array and the item's index. The length of an array is the total number of items in a given array. Arrays are static, the size of an array is established when the array instance is created. This size can't be changed during the lifetime of the instance. We declare and instantiate a single-dimensional array in C# in the following way.

var items = new int[5];
items[0] = 1;
items[1] = 2;
items[2] = 3;
items[3] = 4;
items[4] = 5;

Console.WriteLine(items[2]);

We can iterate through the array using a foreach loop.

foreach (var number in items)
{
    Console.WriteLine(number);
}

In C# arrays are reference types. Reference types keep a reference, in the program execution stack, and that reference to an object located on the dynamic memory (called also heap). The elements of a reference type are always stored in the heap.

memory-msahebhonar.com

An array in C# has the capability to do a tremendous number of different things. I will use arrays as the backbone to implement the other data structures.

You can download the source code on Github.