You are on page 1of 7

Arrays

Until now if we want to store multiple values in a variable, it is not possible to store all
of them in a single variable.
But instead of declaring so many individual variables, we can declare one array variable
which is capable of storing multiple values of single datatype.

Array is a user-defined datatype of reference type and is used to store multiple values of
same datatype sequentially in a single variable.
Size of an array once defined cannot be changed and hence it is said to be of fixed size.
Once an array is defined, the values in the array are stored sequentially in the memory
location.
Each and every element of an array is initialized automatically with a default value of
that datatype.

If it is an integer array, all the elements init are initialized to 0.


If it is Boolean array, all the elements are initialized to false.
If it is character array, all the elements are initialized to null character.
If it is string array, all the elements are initialized to null

Arr_Fig1
Since arrays are of reference type, these are stored in the heap memory.

Arrays in C# are generally classified into 3 types.

1. Single Dimensional Array


2. Multi Dimensional Array
3. Jagged Array

Single Dimensional Array

Single dimensional array is a linear array which can store the data in a single row

Let us see the syntax of array declaration:

Syntax:
datatype[] ArrayName = new datatype[size];
Here new is an operator which will create the array and initialize all its elements with
their default.
size is the size of the array which is fixed once defined.

for ex:
int[] Numbers = new int[5]; where 5 is the length of the Numbers[] array

In this case the array elements are initialized with default value 0.
5 is the size of the array which is fixed once defined.

If we want to access or retrieve individual item or value in an array, we have to use


index of that array i.e., index of an array starts from 0 to n-1, where n is the length of
that array

Here the Numbers[5] array contains the elements from Numbers[0] to Numbers[4].

Let us see the syntax of storing values in an array:


ArrayName[index] = value;

For ex:
Numbers[0] = 10;
Numbers[1] = 30;
Numbers[2] = 80;
Numbers[3] = 100;
Numbers[4] = 70;

Here the default value 0 is overwritten by the values with which we have initialized the
array elements.

Inline Initialization

Case 1:
If we have fixed values to store in an array, then we can declare an array as follows:

For ex:
Int[] array1 = new int[4] {3, 6, 8, 2};

Here the size mentioned is 4. If we pass the number of elements less than or greater
than the length or size of array, then it will give you syntax error.

Hence we need to pass same number of elements mentioned in the size of array.

Case 2:
Without specifying number of elements or size of an array we can even declare an array
using inline initialization.

For ex:
Int[] array1 = new int[] {10, 2, 6, 4};

Here we have not specified any size, so we can pass as many elements as we want but
once if we passed, that will be the size of that array and later cannot be changed.

using System;

class program
{
public static void Main()
{
int[] array1 = new int[5];
array1[0] = 10;
array1[1] = 20;
array1[2] = 40;
array1[3] = 60;
char[] array2 = new char[4] {'a', 'n', 'k', 'o'};
float[] array3 = new float[] { 2.13F, 5, 9.198F, 10.0F, 12};
Console.WriteLine("Second element in array1 is {0}", array1[1]);
Console.WriteLine("Fifth element in array1 is {0}", array1[4]);
Console.WriteLine("First element in array2 is {0}", array2[0]);
Console.WriteLine("Third element in array3 is {0}", array3[2]);
Console.ReadLine();
}
}

OUTPUT
Arr_Fig2

Analysis
array1[] is an integer array whose size is 5
Here only 4 elements got initialized
5th element is not initialized
When we try to print second and fifth elements,
As usual second element is array1[1] =20
And fifth element array1[4]=0

array2[] is an array of type char which is having 4 elements.


array2[0] is the first element in array2 i.e., a

array3[] is an array of type float which is having 5 elements.


array3[2] is the fifth element in array3 i.e., 9.198
Multi dimensional Array
An array with more than one dimension is said to be multi dimensional array.

Syntax:
For two dimensional array
<datatype>[,] = new <datatype>[size];

For three dimensional array


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

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 the sense 2rows and 3 columns

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


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

Accessing two dimensional 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 multi dimensional array

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
Arr_Fig3

Analysis
Here we have taken two dimensional integer array having 2 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 array is ar.Length whose output is 8
And finally the rank, nothing but number of dimensions in the array is ar.Rank which is
obviously 2.

Jagged Array

A Jagged array is an array whose elements are also array i.e., it is an array of arrays.
These element arrays inside a jagged array can be of different dimensions and even can
be of different sizes.

A Jagged array can be declared and initialized as follows:

Int[][] JaggedAr = new int[2][];

Here size 2 represents the number of arrays i.e., two arrays in JaggedAr[][]

We have to initialize the arrays inside before using the Jagged array

JaggedAr[0] = new int[]{5,9,6};


JaggedAr[1] = new int[]{2,5};

Let us see an example code demonstrating on Jagged arrays


using System;

class program
{
public static void Main()
{
string[] NameOfEmployee = new string[2];
NameOfEmployee[0] = "John";
NameOfEmployee[1] = "Winson";

string[][] JaggedAr = new string[2][];


JaggedAr[0] = new string[] { "Bachelors", "masters", "Doctrate" };
JaggedAr[1] = new string[] { "Bachelors", "Masters" };
for(int i = 0;i<JaggedAr.Length;i++ )
{
Console.WriteLine(NameOfEmployee[i]);
Console.WriteLine("-------");
string[] arrayin = JaggedAr[i];

for(int j = 0; j< arrayin.Length;j++)


{
Console.WriteLine(arrayin[j]);
}
Console.WriteLine();
}

Console.ReadLine();
}
}

OUTPUT
Arr_Fig4

Analysis

NameOfEmployee[] is a string array in which there are two elements

JaggedAr[][] is a jagged array having two string arrays


JaggedAr[0] is having three elements ------ Bachelors, masters, Doctorate
JaggedAr[1] is having two elements in it ----- Bachelors, Masters
When i = 0,
since JaggedAr.Length is 2, 0<2 returns true
Control enters parent loop and prints NameOfEmployye[0] which is

John
------
Now JaggedAr[0] is stored in arrayin[]

When j=0,
j<arrayin.Length i.e.,0<3 since JaggedAr[0] is a having three elements
0<3 returns true
So it prints
Bachelors

Again j++, i.e., 1


Now 1<3 returns true
It prints Masters

Again j++, i.e., 2


Now 2<3 returns true
It prints Doctrate
Again j++, i.e., 3
Now 3<3 returns false come out of child loop

Now Console.WriteLine() is executed


Again it increments i in parent loop
This time i=1, so 1<2 returns true
So enters the loop and prints NameOfEmployee[1] i.e.,

Winson
----------

arrayin = JaggedAr[1]
j=0 and j<arrayin.Length which is 2 returns true
Enters child loop and prints arrayin[0]
Bachelors

J++ = 1 and 1<2 returns true


Enters child loop and prints arrayin[1] i.e.,
Masters

J++ = 2 and 2<2 returns false so comes out of the child loop and executes
Console.WriteLine();

Now increments I by 1 so i++ = 2


2<2 returns false so comes out of the parent loop.

You might also like