- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Custom Exceptions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
- C# - Lambda Expressions
- C# - Async and Await
- C# Modern Features
- C# - Tuples
- C# - Records
- C# - Pattern Matching Enhancements
- C# - Top-level Statements
- C# - Nullable Reference Types
- C# - What's New in C# 11 / 12 / 13
- C# - Global Usings
- C# - File-Scoped Namespaces
- C# Practical & Advanced Usage
- C# - JSON & XML Handling
- C# - Data Serialization & Deserialization
- C# - REST API Calls with Httpclient
- C# - Dependency Injection
- C# - Unit Testing with NUnit, xUnit & MSTest
- C# - Package Management with NuGet
C# - Jagged Arrays
A Jagged array is an array of arrays, meaning that the elements of a jagged array are arrays themselves. Unlike a multidimensional array, a jagged array can contain an array of different lengths. You can declare a jagged array named 'scores' of type int as follows −
Jagged Arrays Declaration
You can declare a jagged array by specifying the number of rows, but the columns can vary for each row.
int [][] scores;
Declaring an array does not allocate space for it in memory. To create the array, you need to follow the syntax below −
int[][] scores = new int[5][];
for (int i = 0; i < scores.Length; i++) {
scores[i] = new int[4];
}
Jagged Arrays Initialization
A jagged array can be initialized by providing values for each row individually, the number of elements can be different in each row.
You can initialize a jagged array as follows −
int[][] scores = new int[2][]
{
new int[] { 92, 93, 94 },
new int[] { 85, 66, 87, 88 }
};
Here:
-
scoresis a jagged array containing two arrays of integers. -
scores[0]is an array of 3 integers:{ 92, 93, 94 }. -
scores[1]is an array of 4 integers:{ 85, 66, 87, 88 }.
Example of a C# Jagged Array
The following example demonstrates how to declare, initialize, and print elements of a jagged array:
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
/* a jagged array of 5 array of integers*/
int[][] a = new int[][]{new int[]{0,0},new int[]{1,2},
new int[]{2,4},new int[]{ 3, 6 }, new int[]{ 4, 8 } };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++) {
for (j = 0; j < 2; j++) {
Console.WriteLine("a[{0}][{1}] = {2}", i, j, a[i][j]);
}
}
Console.ReadKey();
}
}
}
When executed, this program outputs:
a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
Iterating Over a Jagged Array
You can iterate over a jagged array using either a for loop or a foreach loop.
Example: Using a for Loop
In the following example, we use a for loop to iterate over a jagged array and display its elements:
using System;
class Program
{
static void Main()
{
int[][] jaggedArray = new int[][]
{
new int[] {1, 2, 3},
new int[] {4, 5},
new int[] {6, 7, 8, 9}
};
for (int i = 0; i < jaggedArray.Length; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
Console.Write(jaggedArray[i][j] + " ");
}
Console.WriteLine();
}
}
}
When the above code is compiled and executed, it produces the following result −
1 2 3 4 5 6 7 8 9
Example: Using a foreach Loop
In the following example, we use a foreach loop to iterate over a jagged array and display its elements:
using System;
class Program
{
static void Main()
{
int[][] jaggedArray = new int[][]
{
new int[] {1, 2, 3},
new int[] {4, 5},
new int[] {6, 7, 8, 9}
};
foreach (int[] array in jaggedArray)
{
foreach (int num in array)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
}
}
When the above code is compiled and executed, it produces the following result −
1 2 3 4 5 6 7 8 9
Jagged Arrays vs. Multi-Dimensional Arrays
The following table shows the key differences between jagged arrays and multi-dimensional arrays:
| Jagged Array | Multi-Dimensional Array |
|---|---|
| Jagged arrays are more optimized, as it only allocates memory for the required elements. | Multidimensional array can waste memory because memory required for all rows (with same length). |
| In Jagged array, you can assign different number of elements in each row. | In Multidimensional array, all rows must have the same number of elements. |
The declaration of a jagged array is: int[][] jagged;
|
The declaration of a multidimensional array is:int[,] multiDim;
|
An element from a jagged array can be accessed using: array[i][j]
|
An element from a multidimensional array can be accessed using: array[i, j]
|