Home > C# > C# Arrays

Arrays are used to store fixed size elements of same data types in sequential order. Since an array is a collection of data but arrays can be also considered as a collection of variables of same data type and placed in consecutive memory locations. Instead of creating individual variables for each part of data we can create a single array to store those parts of data. Arrays in C# are also used to store data of the same type in consecutive memory locations, thus allowing us to access each element if we know the address of any element in the array.
E.g.
To store a data which has 100 parts. Or we can say that we need to store 100 numbers. We need to create hundred 100 individual variables for each number like:
num1, num2, num3 …. num100. But we can create a single C# array to store these 100 numbers.

Declaring Arrays in C#

We can declare an array using the following syntax:

Data-type [] array-name;

Datatype is the type of data which we can store in an array.
[] we use these in declaring arrays. They specify the size of the array.
Array-Name is the name of an array following the same rules as for Naming a variable.

E.g.

int [] my_arr; // Here an array (my_arr) of integer type is declared.

Initializing Arrays in C#

When an array is declared it does not get any space in memory location unless it is initialized. As we know that an array is the reference type. Because each index of the array is pointing to a memory location. So we need to use the new keyword to initialize an instance of the array.
Its syntax is:

Data-type [] array-name = new Data-type [array-size];

array-size specifies the total number of values we can store in an array.
E.g.

int [] my_arr = new int [5]; // Here an array of integer type and of size 5 is initialized.

Different variations in Arrays declaration and initialization are:

double [] my_arr = new double [100];
my_arr [0] = 1500.0;

we can assign values to the array at the time of declaration, as shown below:

double [] my_arr = {340.0, 123.69, 321.5};

An array can also be initialized and values can be assigned using a single statement.

int [] my_arr = new int [3] {1, 2, 3};

An array can be initialized without specifying its size as:

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

one array values can be copied to another array as:

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

How to access array elements?
We can access array elements using indexes. Like C++ arrays, starting index of arrays in C# is also 0. I.e. the first element of an array is at 0th index and we can access it using the 0th index like:

my_arr [0]; // this will point to first element of array.
Similarly, the last index of array will be my_arr [array-size -1]; 

As for an array initialized above. My_arr [4] will point to the last element of the array.
Below example will show working with arrays.

C# Array Example

using System;

namespace arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] my_arr = new int[5];
            my_arr[0] = 05;
            my_arr[1] = 15;
            my_arr[2] = 25;
            my_arr[3] = 35;
            my_arr[4] = 45;

            Console.WriteLine("Element at 0th index is : " + my_arr[0]);
            Console.WriteLine("Element at 1st index is : " + my_arr[1]);
            Console.WriteLine("Element at 2nd index is : " + my_arr[2]);
            Console.WriteLine("Element at 3rd index is : " + my_arr[3]);
            Console.WriteLine("Element at 4th index is : " + my_arr[4]);
        }
    }
}

Output
arrays

Looping through array elements
We use different loops to iterate through the elements of the array.
For loop
For loop allows us to store values into an array and also to access them. Below example shows how a for loop iterates through an array. Look at the following C# array example to know how we can loop through array elements using for loop.

using System;

namespace arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] my_arr = new int[5];
            for(int i=0;i<5;i++)
            {
                Console.WriteLine("Please Enter Value for index " + i +": ");
                my_arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            for(int i=0;i<5; i++)
            {
                Console.WriteLine("Element at index "+i+" is : " + my_arr[i]);
            }
        }
    }
}

Output
c# array example  using for loop

foreach loop
Just like for loop we can use the foreach loop to access elements of an array. As already discussed for each loop iterates through an array in sequential order i.e. it will go through each element of an array. While in for loop we can jump to an index of an array as per requirements. Have a look at the following C# array example.

using System;

namespace arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] my_arr = new int[5];
            for(int i=0;i<5;i++)
            {
                Console.WriteLine("Please Enter Value for index " + i +": ");
                my_arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            foreach (int x in my_arr)
            {
                Console.WriteLine("Elements in array are "+x);
            }
            
        }
    }
}

Output
arrays in c# example using foreach loop

Some other ways to use arrays include :

Leave a Reply

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