Advertisement
type[,] array-name;
where:
//C# Example of two-dimensional array
using System;
class A
{
public static void Main()
{
//Declaration of a 2-D array, which will hold arrays of int values.
int[,] twoDA1;
//Declaring a 2-D array holding arrays of object references of the type class A.
A[,] twoDA2;
}
}
type[,] array-name = new type[first-dimension , second-dimension]
where:
//C# Example of constructing a two-dimensional array
using System;
class A
{
public static void Main()
{
//A 2D Array is constructed on the heap with a size to hold 3 int arrays.
//Where each array has 3 elements in it.
int[,] twoDA1= new int[3,3];
}
}
Array type | Default value |
---|---|
char | '\u0000' |
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0 |
double | 0 |
bool | false | String | null |
Object | null |
Advertisement
//C# Example of initialization of a two-dimensional array
using System;
class A
{
public static void Main()
{
//A 2D array holding 3 int arrays(each array holding 3 int values).
int[,] twoDA= new int[3,3];
twoDA[0,0]=10; //inserting 10 in the first(0th index) array at its 0th index
twoDA[0,1]=20; //inserting 20 in the first(0th index) array at its 1st index
twoDA[0,2]=30; //inserting 30 in the first(0th index) array at its 2nd index
twoDA[1,0]=40; //inserting 40 in the second(1st index) array at its 0th index
twoDA[1,1]=50; //inserting 50 in the second(1st index) array at its 1st index
twoDA[1,2]=60; //inserting 60 in the second(1st index) array at its 2nd index
twoDA[2,0]=70; //inserting 70 in the third(2nd index) array at its 0th index
twoDA[2,1]=80; //inserting 80 in the third(2nd index) array at its 1st index
twoDA[2,2]=90; //inserting 90 in the third(2nd index) array at its 2nd index
}
}
twoDA[0,0]=10;
The index of the first array in our 2D array is specified by the first-dimension i.e. zero, followed by a comma, followed by the
index position in the first array where we are inserting a value, specified by the second-dimension i.e. zero, which
specifies the 0th index of the first array at which we are inserting an int value 10.twoDA[0,1]=20;
We are inserting an int value(20) in the first array at its 1st index.twoDA[0,2]=30;
We are inserting an int value(20) in the first array at its 2nd index.twoDA[1,0]=40;
We are inserting an int value(40) in the second array at its 0th index.twoDA[1,1]=50;
We are inserting an int value(50) in the second array at its 1st index.twoDA[1,2]=60;
We are inserting an int value(60) in the second array at its 2nd index.Console.WriteLine(twoDA[1,0]); //Reading the first(1st index) array and its zeroth(0th index) element
//C# Example of reading a 2-D array holding arrays of primitive values.
using System;
class A
{
public static void Main()
{
//Constructing a 2-D array holding 3 int arrays(each array holding 3 int values).
int[,] twoDA= new int[3,3];
twoDA[0,0]=10; //inserting 10 in the first(0th index) array at its 0th index
twoDA[0,1]=20; //inserting 20 in the first(0th index) array at its 1st index
twoDA[0,2]=30; //inserting 30 in the first(0th index) array at its 2nd index
twoDA[1,0]=40; //inserting 40 in the second(1st index) array at its 0th index
twoDA[1,1]=50; //inserting 50 in the second(1st index) array at its 1st index
twoDA[1,2]=60; //inserting 60 in the second(1st index) array at its 2nd index
twoDA[2,0]=70; //inserting 70 in the third(2nd index) array at its 0th index
twoDA[2,1]=80; //inserting 80 in the third(2nd index) array at its 1st index
twoDA[2,2]=90; //inserting 90 in the third(2nd index) array at its 2nd index
//Checking the total numbers of elements in our 2D array
//By using the Length property of array
Console.WriteLine("Length of our 2D array is : " + twoDA.Length);
Console.WriteLine("Contents of this array are : ");
//Reading our 2D array by using nested for-loops
for(int i=0; i<3; i++)
{
for(int j=0; j<3;j++)
{
Console.WriteLine(twoDA[i,j]);
}
}
}
}
Length of this array is : 9
Contents of this array are :
10
20
30
40
50
60
70
80
90
Advertisement
//C# 2-D Array holding arrays of object references
using System;
class A
{
public static void Main()
{
//Constructing a 2D array holding 2 arrays(each array holding 2 references to objects of type A).
A[,] twoDA= new A[2,2];
twoDA[0,0]= new A(); //inserting an object of A in the first(0th index) array at its 0th index.
twoDA[0,1]= new A(); //inserting ab object of A in the first(0th index) array at its 1st index.
twoDA[1,0]= new A(); //inserting an object of A in the second(1st index) array at its 0th index.
twoDA[1,1]= new A(); //inserting an object of A in the second(1st index) array at its 1st index
//Getting the total length(total number of arrays in a 2D array).
Console.WriteLine("Number of arrays in this 2-D array : " + twoDA.Length);
//Reading the elements of a 2D array by using nested for loops
for(int i=0; i<2; i++)
{
for(int j=0; j< 2;j++)
{
Console.WriteLine(twoDA[i,j]);
}
}
}
}
Number of arrays in this 2-D array : 4
A
A
A
A
//C# A 2D array holding object references will be automatically initialized to null
using System;
class A
{
public static void Main()
{
//Constructing a 2D array holding 2 arrays(each array holding 2 references to objects of type A).
A[,] twoDA= new A[2,2];
//Getting the total length(total number of arrays in a 2D array).
Console.WriteLine("Number of arrays in this 2-D array : " + twoDA.Length);
//Reading the elements of a 2D array by using nested for loops
for(int i=0; i<2; i++)
{
for(int j=0; j<2;j++)
{
Console.WriteLine(twoDA[i,j] + "A null element");
}
}
}
}
A null element
A null element
A null element
A null element
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement