Arrays in C#

Arrays are a set of similarly typed values stored sequentially either as a single dimension, two dimensions or jagged.



Definition:

Arrays are a set of similarly typed values stored sequentially either as a single dimension, two dimensions or jagged.

In C# arrays can be declared as fixed length or dynamic. Fixed length can store a predefined number of items and a dynamic size array increases as you add new items to arrays.

Single Dimension Array:

These are arrays which store data in the form of arow.

Int[] arr=new int[4];
Or
Int[] arr;
arr = new int[5];
or
int[] arr={ list of values}
using system
class sd=array
{
Static void main()
{
Int [] arr= new int[6];
arr[0]=10; arr[1]=20; arr[2]=30; arr[3]=40; arr[4]=50; arr[5]=60;
for(int i=0; i<6 ; i++)
console.writeline(arr[i] + "");
console.writeline();
foreach(int I in arr)
console.writeline(I + "");
}

It was specially design for processing of values in an array or a collection. For each iteration of the loop one value of the array is assigned to the variable of loop. In the case of a for loop of a variable each iteration refers to the index of the array but in the case of a foreach loop it always refers to the value of your array. In a for loop, the loop variable will always be an int only, whereas in the case of a foreach loop, the loop variable will be the same as the type of the value in the array.

An array is a reference type, because it requires dynamic memory management. The size of an array can be specified after declaration also.

Assigning values to array at declaration

Int[] arr={100,200,300}

Memory allocation of array gets performed either with use of new operator or by assigning values to the array at the time of it's declaration.

Array class:

Some properties and methods that can be applied on an array:

-sort(array)
-Reverse(array)

Copy(src,destination,n)
Getlength(int)
Length
Static void main()
{
Int[] arr={10,20,30,40}
For (int i=0;i<arr.length;i++)
Console.write(arr[i] + "");
Console.writeline();
Array.sort(arr);
For each int I in arr)
Console.write(I + "");
Console.writeline();
Array.reverse(arr)
For each (int I in arr)
Console.writeline(I + "");
Console.writeline();
Int[] brr=new int[10];
Array.copy(arr,brr,5);
For each(int I in brr)
Console.write(I + "");
Console.writeline();

Two Dimensional Array:

These are arrays which represent data in the forms of rows and columns.

Int [,]=new int[3,4];
Or
Int[,] arr;
Arr=new int[2,3];

We can identify each and every cell by using row and column index as following:

Int [,] arr=new int[4,5];
Arr[0,0]=5;
Arr[0,1]=10;

We can use getlength method to find no. of rows and column as following:

Arr.getlength(0) for Rows
Arr.getlength(1) for columns
Class tdarray
{
Static void main()
{
Int a=5;
Int[,] arr=new int[4,5];
For( int i=0;i<arr.getlength(0);i++)
{
For(int j=0;j<arr.getlength(1);j++)
{
Arr[I,j]=a;
a+=5;
}}
//printing
For( int i=0;i<arr.getlength(0);i++)
{
For(int j=0;j<arr.getlength(1);j++)
Console.write(arr[I,j] + "");
Console.writeline();
}
}
}

Assigning values to a 2D array at the time of declaration:

Int[,] arr= { {11,12,13,14},
{21,22,23,24},
{31,32,33,34},
{41,42,43,44},
};

Jagged Array

These are the same as two dimensional arrays whereas in two dimensional arrays the number of columns for each row is fixed but in a jagged array the number of columns for each row can vary.

They are also known as an array of arrays because they are like a multiple single dimensional array combined together to form a new array.

Int[][] arr=new int [3][];
Or
Int[][] arr={list of values}

In the case of a jagged array, in the initial declaration we can only specify the number of rows in the array but not columns. After specifying the number of rows now pointing to each row we need to specify the number of columns under row.

Int[][] arr=new int[4][];
arr[0]=new int[6];
arr[0]=new int[8];
arr[0]=new int[7];
arr[0]=new int[5];
arr[0]
arr[1]
arr[2]
arr[3]
class jaggeddemo
{
static void main
{
Int[][] arr=new init[4][];
arr[0]=new int[6];
arr[1]=new int[8];
arr[2]=new int[7];
arr[3]=new int[5];
//Assigning values to array
For(int i=0; i<arr.getlength(0);i++)
arr[i][j]=j+1;
}
//prnting values from jagged array
For(int i=0; i<arr.getlength(0);i++)
{
For(int j=0; j<arr[i].length;j++)
Console.writeline(arr[i][j] + "");
Console.writeline();
}}}
Console.writeline(arr[i][j] + "");
Console.writeline();
}}}

Assign value to jagged array while declaring:

Int[][] arr=
{
New int[3]{11,12,13}
New int[5]{11,12,13,14,15}
};