Posted on 4/22/2025 7:43:53 AM by Admin

Data Structures - Arrays in C#

Now that you are familiar with the concept of Time complexity and the Big O notation, it’s time to actually delve into data structures and algorithms and learn how to use and implement them. In the next few articles, we will explore the data structures in C#. Let’s start with arrays.

Arrays are the most fundamental data structures that lay the foundation for many other structures. It is a structured way of storing multiple values of the same data type in a contiguous memory block. Understanding arrays is really important to work with more complex data structures.

What is an Array?

An array is the collection of homogeneous data elements stored in a contiguous memory block. It holds a fixed-size sequence of elements with the same data type. The important thing to understand here is that the elements are stored back-to-back in the memory, meaning that one element comes after another. Here’s how an array looks like in the memory:

How arrays look like in memory
Array in Memory

Arrays are specifically useful in situations where you want to declare multiple values of the same type. Instead of declaring a separate variable for each value, you declare a single variable (array) and store all the values in it.

Array Declaration and Initialization

In most programming languages, you must declare an array before calling or using it. C# is one of them. Therefore, it provides numerous ways to declare an array:

Declaring an Array

Array declaration refers to assigning a name and data type to the array. The most commonly used syntax for declaring an array in C# is as follows:


datatype[] array_name;

Example:


int[] scores;

Another syntax includes the use of the new keyword:


datatype[] array_name = new datatype[size];

Example:


int[] scores = new int[10];

Array Size

The size of an array refers to the number of elements in it. For instance, consider we have an array of numeric elements from 1 to 10. The size of the array would be 10.

Initializing an Array

Defining the values for an array is called array initialization. C# provides you with various options to initialize an array. You can assign values to it either at the time of declaration or later in the program. But make sure that you do not assign more values than the array size (we’ll discuss later). We usually define array at the time of declaration as follows:


datatype[] array_name = {value_1, value_2, … , value_N};

Example:


int[] scores = {1, 2, 3, 4, 5, 6};

Another way is using the new keyword:


datatype[] array_name = new datatype[array_size] {value_1, value_2, value_3, … , value_N};

Example:


int[] scores =  new int[5] { 1, 2, 3, 4, 5};

For convenience, we will use the first syntax to work with arrays in this tutorial. You can use any of them as you prefer.

Array Index

The location of an element in an array is known as its index. The index of an array always starts at 0 and goes up to [length of array]-1. Simply put, it is always 1 minus the number of elements in an array. For example, consider the following array:


int[] numbers = {3,4,5,6,7};

The index number of each element is represented in the following table:

Element Index
3 0
4 1
5 2

Accessing an Array Elements

You can access the elements of an array with the index.

Syntax:


Console.Write(array_name[index]);

Example:


using System;
class DataStructures {
  static void Main() {
    int[] scores = {44, 54, 10, 29, 39};
    Console.Write(scores[3]); //prints 29
    Console.ReadKey();
}
}

Operations on Arrays

C# provides various ways to manipulate and work with arrays.

Finding the Length of an Array

The length of an array is defined by the number of elements in it. You can determine it with the Length property.

Syntax:


array_name.Length;

Example:


using System;
class DataStructures {
  static void Main() {
    int[] scores = {44, 54, 10, 29, 39};
    Console.Write(scores.Length); //returns 5
    Console.ReadKey();
}
}

Iterating Through Arrays

You can go through each element in an array by iterating through it using the ‘for’ or ‘foreach’ loop.

‘for’ Loop:

Syntax:


for( datatype initializer = value; initializer <= array_name.Length; initializer++)
{
	//Some code here
}

Example:


using System;
class DataStructures {
  static void Main() {
    int[] scores = {44, 54, 10, 29, 39};
    for(int i=0; i<scores.Length; i++)
    {
        Console.Write(scores[i] + " "); //Prints all elements
    }
    Console.ReadKey();
}
}

‘foreach’ Loop:

Syntax:


foreach( datatype variable in array_name)
{
	//Some code here
}

Example:


using System;
class DataStructures {
  static void Main() {
    int[] scores = {44, 54, 10, 29, 39};
    foreach(int i in scores)
    {
        Console.Write(i + " "); //Prints all elements
    }
    Console.ReadKey();
}
}

It is more efficient to use the foreach loop when you have to access all the elements of an array.

Changing the Elements in an Array

Besides access, you can also change the elements in an array as follows:

Syntax:


array_name[index] = new_value;

Example:


using System;
class DataStructures {
  static void Main() {
    int[] scores = {44, 54, 10, 29, 39};
    scores[1] = 10; //Changes 54 to 10
    Console.Write(scores[1]); //returns 10
    Console.ReadKey();
}
}

Multidimensional Arrays

Arrays can be multidimensional. When an array grows vertically as well as horizontally, it becomes multidimensional. In such arrays, the data is represented in tabular or grid form. The most commonly used multidimensional arrays are 2D arrays. Here’s how a 2D array looks like in memory:

How arrays look like in memory
Multidimensional Array in Memory

Declaring and Initializing Multidimensional Arrays

To declare and initialize the multidimensional array, the following syntax is used:

Syntax:


datatype[ , ] array_name = {{value_1, value_2, … , value_N}, {value_1, value_2, … , value_N}}; 

Example:


using System;
class DataStructures {
  static void Main() {
    int[,] scores = {{1,2,3},{4,5,6},{7,8,9}};    
    Console.ReadKey();
}
}

Accessing the Elements in Multidimensional Arrays

You can access the elements of a multidimensional array by specifying the location of the element. In this case, you must specify the row number and the column number.

Syntax:


array_name[row_number , column_number];

Example:


using System;
class DataStructures {
  static void Main() {
    int[,] scores = {{1,2,3},{4,5,6},{7,8,9}};
    Console.Write(scores[0,1]); //Prints 2    
    Console.ReadKey();
}
}

If we expand on this, arrays are a vast topic to learn about, but we cannot cover it all in one lesson. Therefore, I’ve kept it to the multidimensional arrays. After practicing these basic concepts, learn more about arrays yourself.


Data Structures – Lists in C# (Dynamic Arrays)
Data Structures – Lists in C# (Dynamic Arrays)
Data Structures – Stacks in C# (LIFO)
Data Structures – Stacks in C# (LIFO)