Arrays

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:

data-type[]

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:

 

using System;
namespace SampleProgram {
  class MySample {
    public static void Main(string[] args) {
      int[] arrIntData = {
        10,
        45,
        39,
        53,
        24,
        67,
        79
      };
      int[] arrIntNew = new int[7];
      for (int k = 0; k <  arrIntData.Length; k++) {
        Console.Write($ "{
          arrIntData[k]
        } ");
      }
      Array.Sort(arrIntData);
      Console.WriteLine();
      for (int k = 0; k < arrIntData.Length; k++) {
        Console.Write($ " {
          arrIntData[k]
        } ");
      }
      Array.Copy(arrIntData, arrIntNew, 7);
      Console.WriteLine();
      for (int k = 0; k < arrIntNew.Length; k++) {
        Console.Write($"{
          arrIntNew[k]
        }");
      }
    }

 

The output will be:

10 45 39 53 24 67 79
10 24 39 45 53 67 79
10 24 39 45 53 67 79

 

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:

data-type[ , ]  = new [rows, columns]

 

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

 

using System;
namespace SampleProgram {
  class MySample {
    public static void Main(string[] args) {
      int x = 0;
      int[, ] arrIntData = new int[2, 3] {
        {
          10,
          15,
          19
        },
        {
          23,
          29,
          36
        }
      };
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++)
        {
          Console.Write(arrIntData[i, j] + " "); //read values
        }
        Console.WriteLine();
      }
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
          x += 10;
          arrIntData[i, j] = x; // assign values
        }
      }
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
          Console.Write(arrIntData[i, j] + " "); //read values
        }
        Console.WriteLine();
      }
    }
  }
}

 

The output will be:

10 15 19
23 29 36 
10 20 30
40 50 60

 

 

Related Tutorials