What is Array in C#

Beginner

2019-04-14

What is Array in C# ?

An Array is a group of items stored next each other in memory. It's used as a bag or container of elements.

Arrays are the most common datastructure in most languages. And you will find nearly every high level programming language support arrays by default.

Array is a type of random access datastructures, elements are accessed by index not sequentially.

How Arrays store elements ?

Arrays store elements in consecutively meaning that all elements are stored next to each other. This provides a very powerful accessing mechanism, that is access by Index.


Array Elements Are Stored Consecutively

Each element in the array is store in a specific index and you can access that element if you know it's index directly with no iterating overhead.

Array is Accessed By Index

Example accessing array element by index in C#

int number = arr[index];


Why do we need array ?

Imagine you are a class teacher, you class consists of 5 students. And you want to say every student's name loudly to check if someone is absent today.

How would you do that ?

a simple approach is to have separate variables for each class name, so simple.

string student1 = "John";
string student2 = "Michael";
string student3 = "Ali";
string student4 = "Sara";
string student5 = "Yara";

Console.WriteLine(student1);
Console.WriteLine(student2);
Console.WriteLine(student3);
Console.WriteLine(student4);
Console.WriteLine(student5);

just 5 lines of code for our variables and 5 more lines for saying the students' names.

So easy, But that isn't always the case, what if you have more student, let's say 10, 20 or even 30.

Would you keep a separate string variable for each student name ? That results in 30 lines of code to for each string variable and another 30 line of code to print out

string student1 = "John";
string student2 = "Ali";
...
string studentN = "..."



One solution for this issue to use arrays. With arrays you can group the 30 students in a single container and use it to access each student.


Array Performance

Accessing an element is done through element index, this takes a constant time in array.

Searching for an element requires a program to iterate on each element until it finds the required one. This can take up to all the numbers of elements, Imagine search for an element that is located at end of the array and you start the search iteration from the beginning.

Array Search



Insertion to a specified index takes constant time , while inserting to the next empty position in the array can take up to the number of elements.

Deletion Behaves the same as Insertion.

In some Insertion and Deletion cases, a dynamic array may require resizing, the resizing operation can take up to the number of elements of the array as they all will be shifted and moved to the new location.

Arrays Resizing

Array Usage Example in C#

Following is an example using arrays in C#

Create a string array for all the teacher's student names

Print out each student name.



As you can notice the first element of the array is at index 0, which might sound counter intuitive at a first glance. But the index indicated how much is the offset from the start of the array.

So it makes sense that the first element is at index 0.

Array Alternatives

Many datastructures were built on arrays to solve some specific problems. Some use Arrays as a backbone and others are considered pointer-based implementation.

We will talk more about these datastructures in different upcoming tutorials.

Array vs Linked List

The linked list is similar to arrays as it's also a container of a group of elements. But it has different mechanisms.

Linked list elements are not stored consecutively, instead each element has a pointer that points to the next element position in memory, This removes the overhead of resizing and makes inserting and deleting a simple operation.

Accessing an element takes up to the number of all elements, as it doesn't support indexing ( enhanced versions support indexing )

Array vs Dictionary

Dictionary in datastructure is a mapping between a key and value pairs. You can access an element by it's key not index.

Dictionary also called hash tables in other languages or map as in C++.

Accessing takes constant time in dictionaries.

Array vs Queue

A Queue access paradigm is different, it follows the first-in-first-out approach ( FIFO ).

Elements added first to the queue ( enqueued ) are the elements to be removed first when the queue is accessed ( dequeued ).

Array vs Stack

Stacks follow the last-in-first-out ( LIFO ) approach, Elements added  last to the stack ( pushed ) are the elements to be removed first when the stack is accessed ( popped ).


Algorithms that use Arrays

One common algorithm that depends on arrays is the Binary search algorithm, it works only on sorted arrays and used for elements search.

Binary search algorithm reduces the search time significantly.


Summary

An Array is a group of elements stored together in a consecutive area in memory, it allows direct access.

Arrays can be used with loops to achieve the DRY philosophy.

Arrays are the backbone for many modern datastructures and are used with useful algorithms like Binary Search to reduce the search time.