Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Types of Arrays in C#: Single-dimensional, Multi-dimensional and Jagged Array

Types of Arrays in C#: Single-dimensional, Multi-dimensional and Jagged Array

23 May 2024
Intermediate
3.13K Views
15 min read
Learn via Video Course & by Doing Hands-on Labs

Free C# Course Online

Types of Array in C#: An Overview

Welcome to our comprehensive guide on the various types of arrays in C#. Arrays are fundamental data structures used to store elements of the same data type sequentially. In this article, we'll explore the different types of arrays in C#, their features, and how to effectively utilize them in your programming endeavors

Types of Arrays in C#

There are three types of arrays in C#, which are

  1. Single-dimensional Array in C#
  2. Multi-dimensional Array in C#
  3. Jagged Array in C#

1. Single-Dimensional Arrays

In C#, a single-dimensional array is a data structure that stores elements of the same data type in a linear sequence. Each element in the array is accessed by its index, starting from zero. Single-dimensional arrays are versatile and commonly used for various programming tasks, providing efficient data storage and retrieval. one-dimensional array

Declaring Single dimensional array

int[] arr = new int[10];
double numbers = new double[5];
string[] names = new string[2];

Initializing Single dimensional array

int[] numbers = new int[] { 1, 2, 3, 4, 5, 6,7,8,9,10 };
string[] names = new string[] { "Roy", "Shiv", "Tara", "Urmi", "Luna", "Samay" };

Let’s see the practical implementation of Single-Dimensional Arrays in the following example:

Example

  
using System;
class Program
{
 static void Main()
 {
 // Declare and initialize an array
 int[] numbers = { 10, 20, 30, 40, 50 };

 // Output array elements
 Console.WriteLine("Array elements:");
 for (int i = 0; i < numbers.Length; i++)
 {
 Console.WriteLine(numbers[i]);
 }
 }
}

Explanation

This C# code in the C# Compiler initializes an integer array "numbers" with values {10, 20, 30, 40, 50} and then iterates through the array using a for loop to print each element to the console. It demonstrates basic array declaration, initialization, and traversal in a console application.

Output

Array elements:
 10
 20
 30
 40
 50

Read More - C# Programming Interview Questions

2. Multi-Dimensional Arrays

Multi-dimensional arrays in C# are arrays that hold data in more than one dimension, allowing for efficient storage and manipulation of complex data structures. Unlike one-dimensional arrays, these arrays can be thought of as matrices or tables, providing rows and columns to organize and access elements based on multiple indices. Multi-Dimensional

Declaring Multi-dimensional Array

string[,] names = new string[5, 5];
int[,] numbers = new int[7, 8];

Multi-dimensional Array Initialization

int[,] numbers = new int[4, 3] {{2,3}, {4,5}, {6,7}};
string[,] names = new string[2, 2] {{"Urmi", "Rumi"}, {"Ram", "Bheem"}};

Let’s see the practical implementation of Multi-Dimensional Arrays in the following example:

Example

  
using System;

class Program
{
 static void Main()
 {
 // Define a 2D array
 int[,] myArray = new int[3, 4] {
 {1, 2, 3, 4},
 {5, 6, 7, 8},
 {9, 10, 11, 12}
 };

 // Get the dimensions of the array
 int rows = myArray.GetLength(0);
 int columns = myArray.GetLength(1);

 // Display the array elements
 Console.WriteLine("Array elements:");
 for (int i = 0; i < rows; i++)
 {
 for (int j = 0; j < columns; j++)
 {
 Console.Write(myArray[i, j] + " ");
 }
 Console.WriteLine();
 }
 }
}

Explanation

This code defines a 2D array called myArray with 3 rows and 4 columns. It initializes the array with specific values. Then, it retrieves the dimensions of the array using the GetLength method. The code then uses nested loops to iterate over the array and prints each element on the console. The output displays the array elements in a tabular format, with each row on a separate line.

Output

Array elements:
 1 2 3 4 
 5 6 7 8 
 9 10 11 12

3. Jagged Array in C#

In C#, a jagged array is an array of arrays where each element can hold arrays of different sizes. Unlike regular multi-dimensional arrays, jagged arrays allow for flexible and uneven lengths, making them ideal for storing complex data structures like tables or matrices with varying row lengths. jagged-Array

Declaring and initializing Jagged array.

int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[6];
jaggedArray[0] = new int[] { 4, 6, 8, };
jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };

You can also initialize the array upon declaration like this:

int[][] jaggedArray = new int[][]
{
new int[] { 4, 6, 8, },
new int[] { 1, 0, 2, 4, 6 }
};

You can use the following shorthand form:

int[][] jaggedArray =
{
new int[] { 4, 6, 8, },
new int[] { 1, 0, 2, 4, 6 }
};

Let’s see the practical implementation of Jagged Arrays in the following example:

Example

  
 
using System;class Program
{
 static void Main()
 {
 // Create a multi-dimensional jagged array
 int[][] jaggedArray = new int[3][]
 {
 new int[] { 1, 2, 3 },
 new int[] { 4, 5 },
 new int[] { 6, 7, 8, 9 }
 };

 // Display the elements of the jagged array
 for (int i = 0; i < jaggedArray.Length; i++)
 {
 Console.Write("Row {0}: ", i);
 for (int j = 0; j < jaggedArray[i].Length; j++)
 {
 Console.Write(jaggedArray[i][j] + " ");
 }
 Console.WriteLine();
 }
 }
}

Explanation

This code defines a 2D array called myArray with 3 rows and 4 columns. It initializes the array with specific values. Then, it retrieves the dimensions of the array using the GetLength method. The code then uses nested loops to iterate over the array and prints each element on the console. The output displays the array elements in a tabular format, with each row on a separate line.

Output

Row0:123
Row1:45
Row2:6789
Conclusion

In conclusion, understanding the various types of arrays in C# is essential for efficient programming. Whether it's a single-dimensional array, multi-dimensional array, or jagged array, each type has its unique applications. Mastering these concepts empowers developers to optimize their code and solve complex problems effectively.

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Generative AI For Software Developers Jul 27 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this