Arrays are reference types. An Array is a collection of objects of similar datatypes. Arrays are stored in adjacent memory locations on heap.
Arrays are categorized as follows:
• single-dimensional arrays
• multidimensional arrays or rectangular arrays
• jagged arrays,
//Example Listing 6.1
using System;
namespace QSSTraining
{
public class Program
{
public void Main(string[] args)
{
//int [] counter = new int[] {1, 2};
//(OR)
int[] counter = new int[2];
counter[0]=10;
counter[1]=20;
Console.WriteLine("{0} {1}",counter[0], counter[1]);
}
}
}
Multi Dimensional Arrays are of two types. The are (1) Rectangular Array and (2) Jagged Array
Rectangular Array
Rectangular Array example code
//Example Listing 6.2
using System;
namespace QSSTraining
{
public class Program
{
public void Main(string[] args)
{
//int [,] i = {{10, 20, 30}, {40, 50, 60}};
//(OR)
int[,] i = new int[2, 3];
i[0, 0] = 10;
i[0, 1] = 20;
i[0, 2] = 30;
i[1, 0] = 40;
i[1, 1] = 50;
i[1, 2] = 60;
Console.WriteLine("{0} {1} {2}", i[0, 0],i[0, 1], i[0, 2]);
Console.WriteLine("{0} {1} {2}", i[1, 0], i[1, 1], i[1, 2]);
}
}
}
The Array of Arrays is called a jagged array
int[][] ijag = new int[][] {new int[] {10,20,30,40}, new int[] {11,21,31,41,51,61}};
//Example Listing 6.3
using System;
namespace QSSTraining
{
public class Program
{
public void Main(string[] args)
{
int[][] ijag = new int[2][];
ijag[0] = new int[4];
ijag[0][0] = 10;
ijag[0][1] = 20;
ijag[0][2] = 30;
ijag[0][3] = 40;
ijag[1] = new int[6];
ijag[1][0] = 11;
ijag[1][1] = 21;
ijag[1][2] = 31;
ijag[1][3] = 41;
ijag[1][4] = 51;
ijag[1][5] = 61;
Console.WriteLine("{0} {1} {2} {3} ",
ijag[0][0], ijag[0][1], ijag[0][2],
ijag[0][3]);
Console.WriteLine("{0} {1} {2} {3} {4} {5}",
ijag[1][0], ijag[1][1], ijag[1][2],
ijag[1][3], ijag[1][4], ijag[1][5]);
}
}
}
provides Indexers for easy indexed access to class objects as an array. An Indexer has set and get acessors.
public string this[int iIndex]
{
get { …. }
set { … }
}