The Array class is a predefined class under base class libraries defined inside the system namespace. It is
used to store a collection of data and provides a set of features through members to perform
manipulations on an Array.
Array features:
- Sort ()
- Reverse ()
- Copy (source, destination, n)
- GetLength (int)
- Length
Syntax:
Here, data-type like int, double, float, string, etc are used so the array can store only the respective type
of data based on data-type declared for an array.
Example: Array declaration and assign values.
// Only Declaration
int[] arrIntData = new int[7]
string[] arrStringData = new string[4]
//Declaration and assign of values
int[] arrIntData1 = new int[7]{10,23,34,39,45,67,79}
string[] arrStringData1 = new string[4]{“India”,”Russia”,”Germany”,”France”}
// or can assign as below without mentioning size.
int[] arrIntData2 = {10,23,34,39,45,67,89}
string[] arrStringData2 = {“India”,”Russia”,”Germany”,”France”}
//Accessing data from an Array will be done by using Index.
arrIntData1[0] //gives first element 10
arrIntData1[1] //gives second element 23
arrIntData1[6] //gives last element 79
Example 1:
The output will be:
Two Dimensional Arrays:
The Array containing more than one row is called two-dimensional arrays (2D Array). It is also called as
Rectangular Array.
Syntax:
Example: Array declaration and assign of values.
// Only Declaration
int[ , ] arrIntData = new int[2,3];
//or declaration and with assign of values
int[,] arrIntData = {};
Example 2: Print a 2 D array having 2 rows and 3 columns
The output will be: