Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C# Multi Dimensional Arrays

by suresh

An array with more than one dimension is said to be a multi dimensional array in C#. In C#, two dimensional array is the purest form of the multidimensional arrays. In this section, we discuss the multi dimensional arrays.

C# Multi Dimensional Arrays Syntax

For C# two dimensional array

<datatype>[,] = new <datatype>[size];

For C# three dimensional array

<datatype>[,,] = new <datatype>[size]; 
.
.

The C# two dimensional array is also said to be a rectangular array, and it looks like

Int[,] ar = int[2,3]{{2,3,4}{5,6,7}};

Int[2,3] in a sense 2 rows and 3 columns  

{2,3,4} is the first row, having three columns

{5,6,7} is the second row, having three columns

Accessing the C# 2d array,

ar[0,0] = 2

ar[0,1] = 3

ar[0,2] = 4

ar[1,0] = 5

ar[1,1] = 6

ar[1,2] = 7

which looks like

2 3 4

5 6 7

Let us see an example code to demonstrate the multi dimensional array.

C# Multi Dimensional Array Example

To demonstrate the same, we are using the C# 2d array in this example.

using System;
 
 class program
 {
   public static void Main()
   {
     int[,] ar = new int[2, 4] { { 1, 5, 8, 7 }, { 6, 4, 3, 2 } };

     Console.WriteLine(ar[0, 0] + " " + ar[0, 1] + " " + ar[0, 2] + " " + ar[0, 3]);
     Console.WriteLine(ar[1, 0] + " " + ar[1, 1] + " " + ar[1, 2] + " " + ar[1, 3]);
     Console.WriteLine("Fourth element in first row of ar is ar[0,3]: {0}", ar[0, 3]);
     Console.WriteLine("Number of elements in first dimension: {0}", ar.GetLength(0));
     Console.WriteLine("Number of elements in second dimension: {0}", ar.GetLength(1));
     Console.WriteLine("Number of elements in ar: {0}", ar.Length);
     Console.WriteLine("Number of dimensions in ar: {0}", ar.Rank);
     Console.ReadLine();
   }
 }

OUTPUT

C# Multi DImensional Array 1

ANALYSIS

In this C# example, we have taken a two dimensional integer array having two rows and four columns.

Apart from printing those elements, we have also found out the length of each dimension.

Length of first dimension ar.GetLength{0} gives output 2.

Similarly ar.GetLength{1} is 4.

To know the total length of the array is ar.Length whose output is 8.

And finally, the rank, nothing but the number of dimensions in the array is ar.Rank, which is obviously 2.

Placed Under: C#

  • Dot Net Framework
  • C# Basic Example Program
  • C# Variables
  • C# Constant
  • C# Keywords
  • C# Regular Expressions
  • C# Built in Data Types
  • C# Nullable Types
  • C# Data type Conversion
  • C# Date and Time Format
  • C# Enum or Enumerator
  • C# Value and Reference types
  • C# Operators
  • C# Arithmetic Operators
  • C# Assignment Operators
  • C# Bitwise Operators
  • C# Logical Operators
  • C# Null Coalescing operator
  • C# Relational Operators
  • C# Ternary Operator
  • C# Unary Operators
  • C# If Statement
  • C# If Else Statement
  • C# Else if Statement
  • C# Nested If Statement
  • C# Break Statement
  • C# Continue Statement
  • C# goto statement
  • C# Switch Case
  • C# While Loop
  • C# Do while loop
  • C# For Loop
  • C# Foreach loop
  • C# String Builder
  • C# String
  • C# String Functions
  • C# Array
  • C# Array Functions
  • C# Multi Dimensional Arrays
  • C# Jagged Array
  • C# OOPS Introduction
  • C# Constructor
  • C# Destructor
  • C# Access Modifiers
  • C# Inheritance
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy