C#.Net Arrays

A variable can hold only one literal value. If you want to store 100 different values then you have to declare 100 different variables which is very difficult. To overcome this problem, C# introduced an array. An array is a collection of similar values. An array is a special type of data type which can hold multiple values, and can be accessed using indexes. In Array all elements have to be same data type.

Some important points about arrays:

  1. Arrays are always stored in continuous memory allocation.
  2. An array can store multiple values which can be referenced by a single name followed by an index inside a square bracket.
  3. Array index is started from 0.
  4. An array can be an integer, character or floating data type.
  5. The size of an array must be specified while initialization.
Types of Arrays:
  1. Single dimensional
  2. Multi dimensional
  3. Jagged array

Single dimensional

A single dimensional array is one directional array, which contain only one single row and multiple columns. One-dimensional array can be declared as follows:

int [] array1 = new int [10]; // defining array with size 10. In C#.Net, arrays are initialized with the new keyword.
int [] array2 = { 5, 10, 20, 15, 3 }; // defining array with 5 elements which indicates the size of an array
1D Array

Example 1: Display all array elements using for loop.

int[] arr = new int[5] { 10, 20, 30, 40, 50 };

for(int i = 0; i <arr.Length; i++)
    Console.WriteLine(arr[i]);

Multi-dimensional

The Multi-dimensional arrays is a two dimensional array, which contain multiple rows and multiple columns.

For example the following table represents the two dimensional arrays, which contain 3 rows and 5 columns (3 x 5), it has total of 15 elements.

     C0   C1   C2   C3   C4
R0   1    2    3    4    5
R1   6    7    8    9    10
R2   11   12   13   14   15

Syntax for declaring a two-dimensional array is:

int[,] array1 = new int [3, 5];
int[,] array2 = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} };

Example 2: Display 2-dimensional array elements using for loop.

static void Main(string[] args)
{
    int i, j;
    for(i=1;i<=5;i++) //line,row
    {
        for(j=1;j<=i;j++) // cols
        {
            Console.WriteLine(j);
        }
    }
    Console.ReadLine();
}

Jagged Arrays

A jagged array is an array of an array. Multi-dimensional arrays are said to be rectangular i.e. each row is of same size. It is possible to have jagged arrays, where number of rows may be of different size. A jagged array is initialized with two square brackets [][].

Example 3: Initialize jagged array and display value.

int [ ][ ] jagged = new int [2][ ];
jagged[0] = new int[3] {1,2,3};
jagged[1] = new int[4] {1,2,3,4};
foreach (int[] a in jagged)
{
    foreach (int x in a)
    {
        Console.WriteLine(x);
    }
}

Example 4: Input elements in jagged array and display value.

class Program
{
    static void Main(string[] args)
    {
        int i, j;
        int[][] a = new int[3][];
        a[0] = new int[5];
        a[1] = new int[3];
        a[2] = new int[4];

        Console.WriteLine("Enter number");
        for(i=0; i<=2; i++) //row
        {
            for(j=0; j<a[i].Length; j++) //col
            {
                a[i][j] = Convert.ToInt32(Console.ReadLine());
            }
        }

        for(i=0; i<= 2; i++) //row
        {
            //for(j=0; j<a[i].Length; j++) //col
            foreach(int n in a[i])
            {
                Console.Write("" +  n);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}