C# Array

An array is a collection of similar types of values. Each value in the array is to be called an element. The total number of array elements is called the array size. In this article, we will learn how to implement single dimensional array, a 2-dim array, and a multi-dim array.
 
Code Example of a Single Dimension Array
  1. namespace ArrayDemo  
  2. {  
  3.     //Demo on Single-Dim Array. class Program  
  4.     static void Main(string[] args)  
  5.     {  
  6.         //read the number of students int n;  
  7.         Console.Write("Enter number of students: "); n = Convert.ToInt32(Console.ReadLine());  
  8.         //check n value whether it is greater than 0 or not.   
  9.         if (n > 0)  
  10.         {  
  11.             //declare the arrays  
  12.             string[] Names = new string[n]; int[] Marks = new int[n]; string[] Result = new string[n];  
  13.             //read student names  
  14.             Console.WriteLine("\nEnter " + n + " students names:");   
  15.             for (int i = 0; i < n; i++)  
  16.             {  
  17.                 Console.Write((i + 1) + ": "); Names[i] = Console.ReadLine();  
  18.             }  
  19.             //read student marks  
  20.             Console.WriteLine("\nEnter " + n + " students marks:");   
  21.             for (int i = 0; i < n; i++)  
  22.             {  
  23.                 Console.Write((i + 1) + ": ");  
  24.                 Marks[i] = Convert.ToInt32(Console.ReadLine());  
  25.             }  
  26.             //calculate results  
  27.             for (int i = 0; i < n; i++)  
  28.             {  
  29.                 if (Marks[i] >= 0 && Marks[i] <= 100)  
  30.                 {  
  31.                     if (Marks[i] >= 80) Result[i] = "Distinction";  
  32.                     else if (Marks[i] >= 60) Result[i] = "First Class";  
  33.                     else if (Marks[i] >= 50) Result[i] = "Second Class";  
  34.                     else if (Marks[i] >= 35) Result[i] = "Third Class";  
  35.                     else  
  36.                         Result[i] = "Fail";  
  37.                 }  
  38.                 else  
  39.                     Result[i] = "Invalid";  
  40.             }  
  41.             //display the student names and marks   
  42.             Console.WriteLine("\n\nStudent Details:");  
  43.             for (int i = 0; i < n; i++)  
  44.             {  
  45.                 Console.WriteLine((i + 1) + ". " + Names[i] + " - " + Marks[i] + " - " + Result[i]);  
  46.             }  
  47.         }  
  48.         else  
  49.             Console.WriteLine("N value can't be zero."); Console.Read();  
  50.     }  

 
Code Example of a Multi Dimension Array
  1. namespace MultiDimArrays  
  2. //Demo on Multi-Dimensional Arrays class Program  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.         //Single dimensional arrays int[] x = { 10, 20, 30, 40};  
  7.         Console.WriteLine("Single dimensional array:");   
  8.         for (int i = 0; i < x.Length; i++)  
  9.             Console.Write(x[i] + ", "); //Double dimensional arrays  
  10.         int[,] y = { { 10, 20 }, { 30, 40 }, { 50, 60 } };   
  11.         Console.WriteLine("\n\nDouble dimensional array:");  
  12.         for (int i = 0; i < 3; i++)  
  13.         {  
  14.             for (int j = 0; j < 2; j++) Console.Write(y[i, j] + " ");  
  15.             Console.WriteLine();  
  16.         }  
  17.         //Multi dimensional arrays  
  18.         int[,,] z = { { { 5, 10 }, { 15, 20 } }, { { 25, 30 }, { 35, 40 } }, { { 45, 50 }, { 55, 60 } } };  
  19.         Console.WriteLine("\nMulti dimensional array:");  
  20.         for (int i = 0; i < 3; i++)  
  21.         {  
  22.             for (int j = 0; j < 2; j++)  
  23.             {  
  24.                 for (int k = 0; k < 2; k++) Console.Write(z[i, j, k] + " ");  
  25.                 Console.WriteLine();  
  26.             }  
  27.             Console.WriteLine();  
  28.         }  
  29.         Console.Read();  
  30.     }  


foreach Loop
 
One of the most common usages of the for loop is to iterate through a collection of values (an array). C# offers a simple and easier syntax of for loop called a foreach loop, designed only for such kind of array iterations. 
 
Syntax

foreach (datatype variable in arrayname)
{ -----------;
-----------;
}
 
In the preceding syntax, the loop will be executed once for each value in the array. For every iteration, the values of the array will be assigned to the variable.

For example, the following is a for loop:
  1. int[] nums = { 10, 20, 30};  
  2. for (int i = 0;i < nums.Length; i++)  
  3. {  
  4.     Console.WriteLine(nums[i]));  
  5. }   
You can re-write the preceding example with foreach syntax as follows:
  1. int[] nums = { 10, 20, 30};  
  2. foreach (int n in nums)  
  3. {  
  4.     Console.WriteLine(n);  
  5. }   

Note: The arrayname.Length property gets the size of the array. We explain about the "Array" class in the future.

Jagged Arrays

A two-dimensional array is of rectangular size always. But the jagged arrays are more flexible in sizing them. They may not be rectangular size. To declare them, declare the array size in one pair of brackets [size] and then give empty brackets, because the number of elements that can be stored in each row varies.


Code Example of a Jagged Array

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace JaggedArraysDemo  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             int[][] a = new int[3][]; a[0] = new int[] { 1, 2 };  
  12.             a[1] = new int[] { 3, 4, 5, 6, 7, 8 }; a[2] = new int[] { 9, 10, 11 };  
  13.             for (int i = 0; i < a.Length; i++)  
  14.             {  
  15.                 for (int j = 0; j < a[i].Length; j++)  
  16.                 {  
  17.                     Console.Write(a[i][j] + " ");  
  18.                 }  
  19.                 Console.WriteLine();  
  20.             }  
  21.             Console.Read();  
  22.         }  
  23.     }  

Learn more about Arrays here: Working with Arrays using C#


Similar Articles