By 22/07/2015 13 Comments

Jagged Arrays In C#

  • Jagged Arrays In C# are special types of two-dimensional arrays also called nested arrays.
  • A jagged array is an array of arrays in which the length of each array can be different from other.
  • Elements of a jagged array are other arrays with its own length or sizes and dimensions.
  • Jagged array can be with multiple dimensions such as 2D, 3D etc.

General Syntax:

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

STATEMENTS DESCRIPTION
[ ] [ ] Square brackets indicate that it is a jagged array.
[size] Size bracket indicates the size of an array that means how many rows or inner arrays can be hold by jagged array.

Example – 1, Jagged Array using foreach loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {

   // A string type Jagged Array declaration with size three.
            string[][] programingLanguages = new string[3][];

   // Statements assign each new arrays with its own sizes in each jagged array’s index which is from 0 to 2.
            programingLanguages[0] = new string[3];
            programingLanguages[1] = new string[2];
            programingLanguages[2] = new string[2];

   // Statements assign different elements or values in each inner array’ index.
            programingLanguages[0][0] = "C#";
            programingLanguages[0][1] = "Asp.net";
            programingLanguages[0][2] = "Css3";

            programingLanguages[1][0] = "Javascript";
            programingLanguages[1][1] = "Php";

            programingLanguages[2][0] = "Jquery";
            programingLanguages[2][1] = "Html5";

    // foreach loop iterates/extracts each inner or nested array from jagged array.
            foreach (string[] getInnerArray in programingLanguages)

    // foreach loop iterates/extracts each elements from (getInnerArray).
                foreach (string getInnerArrayElement in getInnerArray)
                    Console.WriteLine(getInnerArrayElement);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        C#
        Asp.net
        Css3
        Javascript
        Php
        Jquery
        Html5
       */
    }
}

Example – 2, Jagged Array using nested for loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string type Jagged Array declaration and initialization once.
            string[][] programingLanguages = { 
                      new[] { "C#", "Asp.net", "Css3" }, 
                      new[] { "Javascript", "Php" }, 
                      new[] { "Jquery", "Html5" } 
            };

            // Outer loop iterates each jagged array’s index.
            for (int jaggedArrayIndex = 0; jaggedArrayIndex < 3; jaggedArrayIndex++)

                // Inner loop iterates each inner/nested array’s index.
                for (int innerArrayIndex = 0; innerArrayIndex < programingLanguages[jaggedArrayIndex].Length; innerArrayIndex++)
                    Console.WriteLine(programingLanguages[jaggedArrayIndex][innerArrayIndex]);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        C#
        Asp.net
        Css3
        Javascript
        Php
        Jquery
        Html5
       */
    }
}






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .