DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 37 of 55
←PreviousPrevNextNext→

C# Multidimensional Arrays

Introduction to Multidimensional Arrays

In C#, a multidimensional array is an array with more than one dimension, such as a 2D array (matrix) or 3D array.

These arrays are especially useful for representing structured data like grids, tables, game boards, or mathematical matrices.

Declaring a 2D Array

A 2D array is declared by specifying two dimensions in square brackets, e.g., int[,] matrix.

You can initialize the array with values directly in a block or allocate it with sizes and assign values later.

Example
using System;

namespace TwoDArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] matrix = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
            };

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
Output
1 2 3
4 5 6
7 8 9

3D Arrays

C# also supports three-dimensional arrays, which can be thought of as arrays of 2D layers.

They are declared using three dimensions in square brackets, e.g., int[,,] cube.

Example
using System;

namespace ThreeDArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,,] cube = new int[2, 2, 2]
            {
                { {1, 2}, {3, 4} },
                { {5, 6}, {7, 8} }
            };

            for (int i = 0; i < cube.GetLength(0); i++)
            {
                for (int j = 0; j < cube.GetLength(1); j++)
                {
                    for (int k = 0; k < cube.GetLength(2); k++)
                    {
                        Console.Write(cube[i, j, k] + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
        }
    }
}
Output
1 2 
3 4 

5 6 
7 8
Test your knowledge: C# Multidimensional Arrays
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 37 of 55
←PreviousPrevNextNext→