C Sharp Arrays

C# Arrays

A group of similar types of elements with a contiguous memory location is called an Array in C#. An array in C# is similar to those in other programming languages like C and C++. Being an object of base type System.Array, array in C# can only store a fixed set of elements and have its index starting from 0.

Advantages of C# Array:

  • Facilitates Code Optimization.
  • Allows random access.
  • Data traversing is easy.
  • Data manipulation is easy.
  • Data sorting is easy.

Disadvantages of C# Array:

  • Fixed-size Array.

C# Array Types:

Arrays in C# programming can be of 3 types:

  • Single Dimensional Array
  • Multidimensional Array
  • Jagged Array

C# Single Dimensional Array:

The square brackets [] are used after the type of the array, to create a single-dimensional array in C#.

Example:

int[] arr = new int[10];

The square brackets, however, can not be placed after the identifier.

Example:

int arr[] = new int[10];

Example:

using System;  
public class Example  
{  
    public static void Main(string[] args)  
    {  
        int[] a = new int[10];//creating array  
        a[0] = 1;//initializing array  
        a[1] = 2;  
        a[2] = 3;  
        a[3] = 4;
        a[4] = 5;  
        a[6] = 7; 
        a[7] = 8;  
        a[8] = 9;  
        a[9] = 10; 
 
        //traversing array  
        for (int i = 0; i < a.Length; i++)  
        {  
            Console.WriteLine(a[i]);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are declaring, initializing and traversing a C# array.

Declaration and Initialization at the same time:

We can initialize an array in C# at the time of declaration in 3 ways:

Example:

int[] arr = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

The size of an array can be omitted.

Example:

int[] arr = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

The new operator can also be omitted.

Example:

int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Example:

using System;  
public class Example  
{  
    public static void Main(string[] args)  
    {  
        //Declaration and Initialization of array  
        int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
 
        //traversing array  
        for (int i = 0; i < a.Length; i++)  
        {  
            Console.WriteLine(a[i]);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are declaring and initializing an array at the same time.

Traversal using foreach loop:

To traverse the array elements we can use the foreach loop that returns all the elements of an array one by one.

Example:

using System;  
public class ArrayExample  
{  
    public static void Main(string[] args)  
    {  
        //creating and initializing array  
        int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
        //traversing array  
        foreach (int i in a)  
        {  
            Console.WriteLine(i);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are traversing an array using a foreach loop.

Please Share