A variable can hold only one literal value. If you want to store 100 different values then you have to declare 100 different variables which is very difficult. To overcome this problem, C# introduced an array. An array is a collection of similar values. An array is a special type of data type which can hold multiple values, and can be accessed using indexes. In Array all elements have to be same data type.
Some important points about arrays:
A single dimensional array is one directional array, which contain only one single row and multiple columns. One-dimensional array can be declared as follows:
int [] array1 = new int [10]; // defining array with size 10. In C#.Net, arrays are initialized with the new keyword. int [] array2 = { 5, 10, 20, 15, 3 }; // defining array with 5 elements which indicates the size of an array
Example 1: Display all array elements using for loop.
int[] arr = new int[5] { 10, 20, 30, 40, 50 }; for(int i = 0; i <arr.Length; i++) Console.WriteLine(arr[i]);
S.No. | Function | Syntax | Description |
---|---|---|---|
Length | int len = arr.Length; | Calculate the number of elements in array. | |
Copy Array | int[] arr = {1,2,3}; int[] arr2=(int [])arr.Clone(); | Use to copy array into another | |
Copy Array | Array.Copy(arr, arr2, 5); | Copy only 5 elements | |
Sorting | Array.Sort(arr); | To arrange numbers, work on numbers as well as on strings. | |
Reverse | Array.Reverse(arr); | To reverse the array elements |
The Multi-dimensional arrays is a two dimensional array, which contain multiple rows and multiple columns.
For example the following table represents the two dimensional arrays, which contain 3 rows and 5 columns (3 x 5), it has total of 15 elements.
C0 C1 C2 C3 C4 R0 1 2 3 4 5 R1 6 7 8 9 10 R2 11 12 13 14 15
Syntax for declaring a two-dimensional array is:
int[,] array1 = new int [3, 5]; int[,] array2 = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} };
Example 2: Display 2-dimensional array elements using for loop.
static void Main(string[] args) { int i, j; for(i=1;i<=5;i++) //line,row { for(j=1;j<=i;j++) // cols { Console.WriteLine(j); } } Console.ReadLine(); }
A jagged array is an array of an array. Multi-dimensional arrays are said to be rectangular i.e. each row is of same size. It is possible to have jagged arrays, where number of rows may be of different size. A jagged array is initialized with two square brackets [][].
Example 3: Initialize jagged array and display value.
int [ ][ ] jagged = new int [2][ ]; jagged[0] = new int[3] {1,2,3}; jagged[1] = new int[4] {1,2,3,4}; foreach (int[] a in jagged) { foreach (int x in a) { Console.WriteLine(x); } }
Example 4: Input elements in jagged array and display value.
class Program { static void Main(string[] args) { int i, j; int[][] a = new int[3][]; a[0] = new int[5]; a[1] = new int[3]; a[2] = new int[4]; Console.WriteLine("Enter number"); for(i=0; i<=2; i++) //row { for(j=0; j<a[i].Length; j++) //col { a[i][j] = Convert.ToInt32(Console.ReadLine()); } } for(i=0; i<= 2; i++) //row { //for(j=0; j<a[i].Length; j++) //col foreach(int n in a[i]) { Console.Write("" + n); } Console.WriteLine(); } Console.ReadLine(); } }