You must have learned C, C++, Java, etc and like any other programming language, an array in C# is also a group or a fixed-size sequential collection of data having the same data-type stored in a contiguous memory location.
The are used to store multiple values in a single variable instead of creating multiple variable for each value. It is a simple data structure format where the primitive type of data such as int, char, double, float, etc are stored and accessed randomly through their index number such as array[0], array[1], etc.
Elements are arranged inside the array with index numbers starting from zero as the first elements.
There are 3 types of arrays in C#:
- Single Dimensional Array
- Multidimensional Array
- Jagged Array
Arrays are objects in C# whose allocation of memory is done dynamically in a program. Since they are objects, we can use predefined functions in C# to find the size of an array.
Declaration of array in C#
For declaration of an array in C#, the user needs to specify the type of element and number of elements in an array. Below is the declaration of the single-dimensional Array.
1 | data-type array_name[arraySize]; |
The data type must be a valid C# data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for an integer array with 20 elements.
1 | int arr[20]; |
Initialization of array in C#
Declaration doe not mean that the array is initialized in a memory. Initialization of an array means assigning the value to an array.
An array is a reference type that is it deals with the address of the elements. And since it is a reference type, we need to use new
keyword in order to initialize the array variable.
Syntax and Example:
1 2 3 4 5 | //syntax data-type [] <array_name> = new <data-type>[size]; //Example int[] numbers = new int[10]; |
This way an instance of an array is created with a new keyword.
You can initialize an array in various ways as follows:
1. Create and initialize an array.
1 | int[] num = new int[5] {12, 88, 4, 32, 48}; |
2. Omit the size of array.
1 | int[] num = new int[] {12, 88, 4, 32, 48}; |
3. Initialize an array at the time of declaration.
1 | int[] num= {12, 88, 4, 32, 48}; |
Initialize the array after multiple array declaration. Example:
1 2 3 4 5 6 7 8 | // Declaration of multiple integer array int[] num1, num2; // Initialization of arrays num1 = new int[5] {12, 88, 4, 32, 48}; num2 = new int[5] {14, 45, 6, 64, 99}; |
Accessing Array Elements in C#
As told above, that each array elements are associated with specific number that is the index numbers.
An array element can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:
1 2 3 | //Accessing single-dimensional Array arr[3]; //the 4th element is accessed arr[7]; //the 8th element is accessed |
You can also assign the value by accessing each of the position through index number. Example:
1 2 3 | int[] num= new int[5]; num[0] = 45; //assigning 45 to the 1st element num[2] = 34; //assigning 34 to the 3rd element |
C# program for initialization, declaration and accessing an array
In the program below we will initialize an array by the user input that is we will take an element of array from the user input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; namespace Arrays { class Program { static void Main(string[] args) { //declaration and initialization an array int[] arr = {5, 10, 15, 20, 25}; //displaying an array. for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Element[{0}] = {1}", i, arr[i]); } } } } |
Output:
1 2 3 4 5 | Element[0] = 5 Element[1] = 10 Element[2] = 15 Element[3] = 20 Element[4] = 25 |
Example: C# array initializing an array by accessing an array with an index number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; namespace Arrays { class Program { static void Main(string[] args) { //Array declaration int[] arr; //allocation of memory for 5 integers arr = new int[5]; //Initialization of an array. arr[0] = 5; arr[1] = 10; arr[2] = 15; arr[3] = 20; arr[4] = 25; //5 elements, index starts from 0 so index is till 4 //displaying an array. for (int i = 0; i < arr.Length; i++) Console.WriteLine("Element[{0}] = {1}", i, arr[i]); } } } |
Output:
1 2 3 4 5 | Element[0] = 5 Element[1] = 10 Element[2] = 15 Element[3] = 20 Element[4] = 25 |
Using the foreach Loop
We used for loop above to display an element of an array one by one. Now we will do the same with foreach loop in C#. Traversal using foreach loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; namespace Arrays { class Program { static void Main(string[] args) { //creating and initializing an array int[] arr = {5, 10, 15, 20, 25}; int j = 0; //traversing to display with foreach foreach (int i in arr) { Console.WriteLine("arr[{0}] = {1}", j, i); j++; } } } } |
Output:
1 2 3 4 5 | arr[0] = 5 arr[1] = 10 arr[2] = 15 arr[3] = 20 arr[4] = 25 |
C# Arrays in Detail
here is more to the arrays in C#. The above understanding mostly for 2D arrays. Click on the topic below to learn in detail about arrays in C#.