Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Arrays:

Arrays are used to store collection of data. It stores a fixed-size sequential collection of
elements of the same data type. Instead of declaring different individual variables, we declare
one array variable. Specific element in the array is accessed by an index.
First Element last Element

Number 1 Number 2 Number 3 ……

Classification of Arrays:
There are three different types of Arrays

1. Single Dimension Array

2. Multi Dimension Array

3. Jagged Array

1. Single Dimension Array


Following example shows the initialization and execution of the single dimension
array

using System;
namespace Array
{
class Program
{
static void Main(string[] args)
{
//There are different ways to initialize arrays in C#
some are as follows:

Console.WriteLine("Array 1 Data\n"); int[] arr1 = new


int[] { 2, 4, 6, 8, 0 };
for (int i = 0; i < 5; i++)
{
Console.Write("Array 1 data at index " + (i + 1));
Console.WriteLine(": " + arr1[i]);
}
Console.WriteLine("\n\nArray 2 Data\n");
int[] arr = new int[4];
arr[0] = 5 ;
arr[1] = 7;
arr[2] = 9;
arr[3] = 3;

foreach (int item in arr)


{
Console.WriteLine("Array Element:" + item);
}
}
}
}

2.

Output:
2. Multi-Dimension Arrays
Following example show the initialization and execution of the multi-Dimension
arrays.

using System;

namespace Array
{
class Program
{
static void Main(string[] args)
{
//there are different ways to initialize arrays in C# some are as follows:

Console.WriteLine("Array 1 Data\n");
int[,] arr = new int[2, 5] { { 0, 2, 4, 6, 8 }, { 1, 3, 5, 7, 9 } };
for(int i=0; i<2;i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write("\t"+arr[i,j]);
}
Console.WriteLine("\n\t");
}

Console.WriteLine("\n\nArray 2 Data\n");
int[,] arr1 = new int[2,3];
arr1[0, 0] = 11;
arr1[0, 1] = 22;
arr1[0, 2] = 33;
arr1[1, 0] = 10;
arr1[1, 1] = 20;
arr1[1, 2] = 30;

for(int i= 0; i < 2; i++)


{
for(int j=0; j<3; j++)
{
Console.Write("\t" + arr1[i, j]);
}
Console.WriteLine("\n\t");
}
}
}
}
Output:

You might also like