We use arrays when we want to store multiple values of the same type under a single variable name. The arrays store the elements in sequential order in the memory. We can access them by their index. The index starts with 0 and end with the length of the array minus 1.
Table of Contents
Example of an Array
In the example, we have an array of integers starting with index 0 and the total number of elements are 6, the last index being 5. The following are various ways to declare/initialize a single dimensional array.
int[] firstArray = {10,20,30,40,50,60 };
int[] secondArray = new int[] { 2,8,45,-9 };
int[] thirdArray = new int[10];
int[] : datatype of the array is initialized/declared.
firstArray : Variable name for the array of integers
{} : Initialize the array with elements by comma as a separator.
‘secondArray’ is declared and initialized by adding a ‘new’ keyword. It sets the values for the array and automatically allocates the length as per the number of values initialized.
‘thirdArray’ is declared with a length of 10. The default values are 0 and reference values are set to null.
Access, Sort and looping Array elements
We access the array element by its index. It can be a number or it can be in an iterative loop. For instance, I will access, sort and display them here.
//Accessing the array elements
int a = firstArray[2];// third element in the firstArray
int b = secondArray[3];// fourth element in secondArray
int result = a + b;
Console.WriteLine("Result: ", result);
//sort the array
Array.Sort(secondArray);
Console.WriteLine("Second Array");
foreach(var item in secondArray)
{
Console.WriteLine(item);
}
Console.WriteLine("First array");
for(int i =0; i <firstArray.Length;i++)
{
Console.WriteLine(firstArray[i]);
}
We use index to access a specific element in the array. For instance, I used firstArray[2] to access the value at index 3. C# provides us with sort functionality which is Array.sort(). We can use foreach and for loop to access elements in the array. An instance of the usage is shown.
Types of Arrays
Arrays are of 3 types. We learned how to declare and initialize a single dimensional array.
- Single dimensional array
- Multi dimensional array
- Jagged array
In this paragraph, I will talk about Multi dimensional array.
Multi-dimensional arrays are like a matrix. A 2D matrix consist of a set of rows and columns. It contains a comma in the type of the data. Here, I declare a 2D array with 2 rows and 3 columns.
//2D array -- a multi dimensional array
int[,] twoDimensional = new int[2,3];
Further, I will talk about Jagged arrays.
A Jagged array is an array of arrays which are possibly of different sizes. We initialize it as follows:
//Jagged array
int[][] jaggedArr = new int[5][];
jaggedArr[0] = new int[9];
jagged[1] = new int[8];
jagged[2] = new int[3];
//.....
In this example, you can see that I have initialized each element ‘jaggedArr’ as different sizes. This is the main difference between a multi-dimensional array and a jagged array. This is a popular interview question. In multi-dimensional array we cannot have a matrix where each row has different sizes of columns. It is more like matrix unlike jagged arrays where it can have different sizes.
I have created a program to run and see how an array works. Feel free to try this code.
using System;
namespace TelestreakTutorial
{
class Program
{
static void ArrayTutorial()
{
int[] firstArray = {10,20,30,40,50,60 };
int[] secondArray = new int[] { 2,8,45,-9 };
int[] thirdArray = new int[10];
//Accessing the array elements
int a = firstArray[2];// third element in the firstArray
int b = secondArray[3];// fourth element in secondArray
int result = a + b;
Console.WriteLine("Result: ", result);
//sort the array
Array.Sort(secondArray);
Console.WriteLine("Second Array");
foreach(var item in secondArray)
{
Console.WriteLine(item);
}
Console.WriteLine("First array");
for(int i =0; i <firstArray.Length;i++)
{
Console.WriteLine(firstArray[i]);
}
//2D array -- a multi dimensional array
int[,] twoDimensional = new int[2,3];
//3D array in C#
int[,,] threeDimensional = {
{
{ 1, 2 },
{ 3,4}
},
{
{5,6 },
{7,8 }
}
};
//Jagged array
int[][] jaggedArr = new int[5][];
jaggedArr[0] = new int[9];
jaggedArr[1] = new int[8];
jaggedArr[2] = new int[3];
}
static void Main(string[] args)
{
ArrayTutorial();
}
}
}
Conclusion
To sum it up, we can use arrays when we know the size in advance. When you want to allocate memory in run-time and do not know the size of the data, you can use ArrayList. ArrayList is my next article. So far I hope you have a good understanding about arrays. Feel free to comment on what you like about arrays and how you use it in your code 🙂