What is Jagged Array in C#?

We have examined around One Dimensional exhibit and Two Dimensional clusters in C#.Net, and we realize that in two-dimensional exhibits, each column has some number of components however all lines will have the same number of components.

In this post, we will find out about Jagged Array, which is not bolstered by C++ programming language.

A rugged exhibit is an extraordinary kind of multidimensional cluster that has the sporadic measurements’ sizes. Each line has a distinctive number of components in it.

Degradation of array

<data_type>[][] variable = new <data_type> [row_size][];

Example

int[][] X = new int[2][];
X[0] = new int [4];
X[1] = new int [6];

Initialization 

int[][] X = new int[][] {new int[] {1, 2, 3}, new int[] {4,5, 6, 7}};

An example of a jagged array in C#

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;

            int[][] X = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 } }; 

            

            Console.Write("\n\nElements are: \n");
            for (i = 0; i < X.GetLength(0); i++)
            {
                for (j = 0; j < X[i].Length; j++)
                {
                    Console.Write(X[i][j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output

Elements are:
1 2 3
4 5 6 7
Press any key to continue . . .

Leave a Comment

error: Alert: Content is protected!!