Why Do We Need Arrays?
Array in C#. It feels like an array is useless in the beginning when you start learning about it but once you have understood the concept of the array in C#, you realize it is very useful. Imagine you need to save the names of a hundred employees. Each name needs one variable. You will need a hundred variables to save a hundred names.
How labor-intensive and boring it would be to create a hundred variables. You will need a hundred names for the variables to save hundred employee names. I know you are either laughing or confused. If you are confused that means you realize that it is a problem and we need a solution for it. The solution to this problem is an array.
What is an Array?
Array in C# is a very important feature provided by Microsoft. An Array in C# is a user-defined collection of homogeneous data types elements. In simple terms, this can save a series of elements that are of the same data types. So you declare an array and save the list of a hundred names in that array. Or you can also save a list of thousands of numbers.
Below is the visual representation of an Array. This is how you can visualize it. Each block will contain one element of an array.

How to save the names and how you access the elements is something you will learn below.
Whenever you are declaring an array you have to declare a number of things about it.
- Array Type
- Array Dimension
- Array Name
- Size
How to declare an Array
There are two approaches to declare an array. In example 1 the first approach is demonstrated and in example 2, the second approach is demonstrated.
Example 1
In this approach of declaring an array you declare and assign the elements to it at the same time.
int[] myArray = {5,10,15};
Example 2
Another approach to declare an array. In this approach, you don’t initialize the array but only declare it. You will have to initialize it in a separate line. When I say initialize I mean saving values in the array.
int[] numbers = new int[3];
numbers[0] = 5;
numbers[1] = 10;
numbers[2] = 15;
This was the example of an integer type array. If you want to create a string type array then you will have to use string keyword instead of int.
String Example
string myArray = new int[3];
myarray = {"John",Jenice, Sheron};
This is how to declare a string array. You will access the elements exactly the same as an int array.
Multidimensional Array in C#
A multidimensional array is also called an array of arrays. In a multidimensional array, we save data in row and columns. When we create a multidimensional array in C#, we need to declare how many rows and columns would be in the array.
int[,] names = new int[3, 4];
The [,] is a symbol of a multidimensional array and int[3, 4] shows this array is going to have 3 rows and 4 columns. Below is the visual representation of a 2D array.

How to Initialize a Multidimensional Array
using System;
using System.Collections;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int[,] names = new int[,] {{ 2,1}, {5,1 },{5,3 },{1,4 },{ 8,2},{ 4,3}};
Console.ReadLine();
}
}
}
Accessing the elements in an Array
The above example 1 shows the array name is myArray and its type is an integer. The element can only contain 3 elements. If you save 3 elements in the array then they will be set at myArray[0], myArray[1], myArray[2].
Support you set 5, 10, 15 as shown in the first example 1 then you will access the first element with myArray[0], the second element with myArray[1], and the third element with myArray[2]. I hope you get the point.
Accessing Elements in Example 2
using System;
using System.Collections;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[3];
numbers[0] = 5;
numbers[1] = 10;
numbers[2] = 15;
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[1]);
Console.WriteLine(numbers[2]);
Console.ReadLine();
}
}
}
Output

Using Foreach Loop
using System;
using System.Collections;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 2,3,4,5,6};
foreach (var item in numbers)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
Output

Accessing the Elements of a Multidimensional Array
The elements of a multidimensional array can be accessed using foreach loop just like we did it in the erlier examples, the foreach will print all the numbers on console screen.
Let me show you how to access the elements one by one.
using System;
using System.Collections;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int[,] names = new int[,] {{ 2,1}, {5,1 },{5,3 },{1,4 },{ 8,2},{ 4,3}};
Console.WriteLine(names[0,0]);
Console.ReadLine();
}
}
}
Output

As you can see in the above example, we wanted to access 2 from the first element so we accessed it by coordinates that are [0,0]. If we wanted to access 1 from (5,1) then we will have to put the coordinates of 1 which are (1,1). Let me show you in the code.
using System;
using System.Collections;
namespace Array
{
class Program
{
static void Main(string[] args)
{
int[,] names = new int[,] {{ 2,1}, {5,1 },{5,3 },{1,4 },{ 8,2},{ 4,3}};
Console.WriteLine(names[1,1]);
Console.ReadLine();
}
}
}
Output

Explanation
The example 1 coding you may have understood easily where we have declared one array of 3 elements. We have used the Console.ReadLine function to print the value of the console.
The foreach loop is specially designed to access the values of all types of arrays. This loop the loop has one variable called item in which the elements of the array will be saved one by one. The collection will be replaced with the name of an array which we have done in this example.
The collection is replaced with array named numbers and the value of each element is printed on console one by one, by using Console.WriteLine function.
The Console.ReadLine(); function is just used to hold the console screen after the loop is finished.
I hope you have understood the concept of array in C# and also understood how to access its elements.
The Drawbacks of Array
- You should know how many elements you would save in an array so that you can mention it at the time of array declaration.
- You can save only one type of element in an array. If you want to save string and integer both in the same array. It is not possible.
I hope you get the point. I will be extending this article shortly.
Stay Safe Stay healthy.
Take Care Guys!