Signup/Sign In
LAST UPDATED: SEPTEMBER 2, 2021

C# Arrays - Single/Multi Dimensional and Jagged Arrays

    In this article, we will cover the concept of the array and its types such as single-dimensional, multidimensional, and jagged arrays. At the end of this tutorial, you will have a short programming exercise based on the concept of an array. Also, in this tutorial, we have used the Length property of array to get the length of an array, which we will cover in details in the next tutorial, where we will discuss in detail about the Array class and how to use various array methods and properties.

    Like any other programming languages, an array is a type of data structure used to store the same type of variables in contiguous memory locations. The elements stored in the array are indexed, starting from 0 (an array with n elements will have index from 0 to n-1). Here is a simple way of declaring an array by specifying the type of elements that will be stored in it,

    type[] arrayName;
    
    // Declare a single-dimensional array
    int[] arr = new int[10];
    
    // Declare a two-dimensional array
    int[,] arr = new int[5, 5];
    
    // Declare a jagged array
    int[][] arr = new int[5][];

    In C# programming, arrays are implemented as objects (of System.Array class). Let's cover some practical examples of the array.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                int[] arr = {10, 11, 100, 101, 110};
                int len = arr.Length;
                Console.WriteLine("Length of Array " + len);
                Console.WriteLine(arr[0] + " " + arr[4]);
            }
        }
    }

    Output:

    Length of Array 5
    10 110

    In the above example, we have created an array and have stored five values to it. Then, we have used the Length property to get the length of that array. Hence, the output shows 5 (the total number of elements in the array) but the index of the array always start from 0 (refer below image).

    c sharp arrays

    To access the data elements stored in an array use, arr[index] where arr is the name of the array variable and index is the index of the element, starting from 0.


    Types of C# Arrays:

    There are three types of arrays in C# programming:

    1. Single-Dimensional Array

    2. Multidimensional Array

    3. Jagged Array

    We will cover all of them, one by one.


    1. Single-Dimensional Array:

    The single-dimension array contains only one row of data, hence the data element stored in the array can be accessed using a numeric index like 0, 1, 2, etc. The following program illustrates a single-dimensional array where we are creating, initializing, and traversing the array using a for and a foreach loop (output will be the same using both the for and foreach loops).

    Filename: Program.cs

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int[] arr = new int[] { 10, 11, 100, 101, 110 };
                for(int i=0;i<arr.Length;i++)
                {
                    Console.WriteLine(arr[i]);
                }
                /*foreach(int a in arr)
                {
                    Console.WriteLine(a);
                }*/
                Console.ReadLine();
            }
        }
    }

    Output:

    10
    11
    100
    101
    110

    We have created an array of 5 elements of type int and then traversed it using a for loop. As we know, the for loop requires three parameters: counter initialization, condition, and counter increment/decrement. Hence, to provide the condition, we used the Length property to calculate the length of the array and print each element accordingly. Like the above example, we can create, initialize, and traverse a string array as well. Below program illustrates the array elements of string type.

    Filename: Program.cs

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                string[] arr = new string[] { "C++", "Java", "C#" };
                foreach(string a in arr)
                {
                    Console.WriteLine(a);
                }
                Console.ReadLine();
            }
        }
    }

    Output:

    C++
    Java
    C#


    2. Multidimensional Array:

    The multidimensional array contains more than one row to store the data, hence its index will be two numbers in pair, one identifying the row and one for the column. This type of array contains the same length of elements in each row hence, it is also known as a rectangular array.

    The following program illustrates the multidimensional array where we are creating an array of 2 X 2 (matrix), by accepting input from the user and displaying the final array.

    Filename: Program.cs

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int i, j;
                int[,] arr = new int[2, 2];
                Console.WriteLine("Enter the elements for 2 X 2 matrix");
                for(i=0;i<2;i++)
                {
                    for(j=0;j<2;j++)
                    {
                        Console.Write("Enter element at [{0},{1}] = ", i, j);
                        arr[i, j] = Convert.ToInt32(Console.ReadLine());
                    }
                }
    
                for (i = 0; i < 2; i++)
                {
                    Console.Write("\n");
                    for (j = 0; j < 2; j++)
                    {
                        Console.Write("{0}\t", arr[i, j]);
                    }
                }
            }
        }
    }

    Output:

    Enter the elements for 2 X 2 matrix
    Enter element at [0,0] = 50
    Enter element at [0,1] = 28
    Enter element at [1,0] = 23
    Enter element at [1,1] = 89
    50 28
    23 89

    In the above program, we have created a multidimensional array of size 2 X 2, and then we have asked the user to input values which are stored in the array. If you want to create a matrix of size 3 X 3, 4 X 5, or more, then you can accept the number of rows and columns from the user and then take input for array values accordingly.

    Using the Console.ReadLine() method, we have accepted input from the user and stored them in the array using iterators i and j at location arr[i, j]. The ToInt32() method is used to convert the specified value to an equivalent 32-bit signed integer.


    3. Jagged Array:

    It is the array whose elements are also arrays and of any dimension and size. It is also known as the array of arrays. Its elements are of reference types and are initialized to null. The following program illustrates the jagged array.

    Note: The elements of a jagged array must be initialized before they are used.

    Filename: Program.cs

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int[][] arr = new int[][]
                {
                    new int[] { 0, 1 },
                    new int[] { 10, 11 },
                    new int[] { 100, 101, 110, 111 }
                };
                for(int i=0; i<arr.Length;i++)
                {
                    for(int j=0;j<arr[i].Length; j++)
                    {
                        System.Console.Write(arr[i][j] + " ");
                    }
                    System.Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    }

    Output:

    0 1
    10 11
    100 101 110 111


    Time for a Quiz!

    Find the output of the below specified 3 C# programs (using Arrays). The answers are available at the end of this article.

    Please don't cheat, be true to yourself.

    Program 1 (Filename: Program.cs)

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int[] arr = { 10, 11, 100, 101, 110 };
                Console.WriteLine(arr[5]);
                Console.ReadLine();
            }
        }
    }

    Program 2 (Filename: Program.cs)

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int i = 0;
                int[] arr = { 0, 1, 10, 11, 100 };
                Console.WriteLine(arr[2]+arr[arr[++i]]);
                Console.ReadKey();
            }
        }
    }

    Program 3 (Filename: Program.cs)

    using System;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                int[,] arr = new int[3,3];
                Console.WriteLine(arr.Length);
                Console.ReadKey();
            }
        }
    }

    The output of the above three programs are:

    Program No. Output

    Program 1

    System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
    Program 2 11 (arr[2]+[arr[++i]]) = (10 + 1 = 11)
    Program 3 9 (multi dimensional array 3 X 3 = 9)

    We hope this article helped you in understanding the concept of arrays in C# programming. In the next article, we will discuss about the C# Array class.

    You may also like:

    Subject Matter Expert of C# Programming at Studytonight.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS