C# Jagged Arrays Tutorial with Examples

C# Jagged Arrays Tutorial with Examples

Jagged Arrays in C#

In this post,we will learn about Jagged Arrays in C# with an example.

Now in this post, I will explain Jagged Arrays in C# with appropriate example. A Jagged array is an array of arrays. Jagged Arrays is also called as “array of arrays” because its elements are arrays. The element size of jagged array can be different.

Types of Array in C#.Net

Arrays can be divided into the following four categories.

Declaration of Jagged array

Declare jagged array that has two elements.

READ :  C# Armstrong Number Tutorial with Examples

int[][] arr = new int[2][];

Initialization of Jagged array

Write below lines to initialize jagged array. The size of elements can be different.

arr[0] = new int[2];  
arr[1] = new int[3];

Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Jagged Array Example

            int[][] _JaggedArray = new int[2][];// Declare jagged array  

            _JaggedArray[0] = new int[] { 11, 21, 31, 41 };// Initialize the array with value       
            _JaggedArray[1] = new int[] { 42, 52, 62, 72, 83, 93 };

            // Raed and print array elements  
            for (int i = 0; i < _JaggedArray.Length; i++)
            {
                for (int j = 0; j < _JaggedArray[i].Length; j++)
                {
                    System.Console.Write(_JaggedArray[i][j] + " ");
                }
                System.Console.WriteLine();
            }
            Console.ReadKey();
        }
    }    
}

OutPut :

11 21 31 41
42 52 62 72 83 93

ASP.NET Example With Tutorial

Read :

READ :  PHP Simple cURL API call using POST

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Jagged Arrays in C#.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

We hope This Post can help you…….Good Luck!.

Add a Comment

Your email address will not be published. Required fields are marked *