Arrays In C# And Why They Are Used

An Array in layman terms is to place a group of things in a particular position so that they are in order. This meaning applies to software field as well. Let's understand what is array and why they are used in coding.






What Is An Array?


An Array is a collection of variables of same data types which are stored at adjacent memory locations. The collection can be of any built-in type from .Net framework or any custom class. We will let you know what's class in our following posts. Array has zero based index system which means the first element would be present at the zeroth position.


Basic Structure Of An Array


Shown below is the pictorial representation of array and its memory allocation.

Arrays take a predetermined number of values, so whenever user initializes an array of some specific type, then the .Net framework allocates memory for all the values as per the data type of array.


Basic structure of an array


How To Initialize An Array?


Whenever we say something is initialized, we understand that the variable has some value and we can access it's value through code. We can initialize array in two ways, first by specifying the size of an array and giving the values to it later on & second by specifying the values directly to it. Shown below are two ways to initialize an array.


//Intializing by giving size of array
datatype[] nameOfArray = new datatype[sizeOfArray];

//Examples
int[] arrayIntegers = new int[5];
arrayIntegers[0] = 55;
arrayIntegers[1] = 22;
arrayIntegers[2] = 102;    //Assigning value to the initialized array
arrayIntegers[3] = 36;
arrayIntegers[4] = 78;



//Initializing by giving values
datatype[] nameOfArray = new datatype[sizeOfArray];

//Examples
int[] arrayIntegers = new int[] {45,85,78,63,41};
string[] arrayString = new string[] {"John", "Tim", "Bruno", "Justin"};



Why We Need Array? 


Suppose we are making an application for a grocery store and we need to store the types of fruits present in the store. So we need text in string form to be shown as data.

string fruit1 = "Apple";
string fruit2 = "Banana";
string fruit3 = "Grapes";
string fruit4 = "Mango";
.
.
so on..

So instead of creating separate variables, we can use array to declare these string variables as shown below

string[] fruits = new string[] {"Apple", "Banana", "Grapes", "Mango"};


And accessing these variables through array is very easy. Arrays are index based and start from 0 index. So value of fruits array at 0 index is Apple, at 1 index is banana and so on. Shown below is way to access the values


fruits[0] //returns Apple
fruits[1] //returns Banana
fruits[2] //returns Grapes
fruits[3] //returns Mango

fruits[0] + " and " fruits[3] //returns Apple and Mango


Use Array In For Loop


Now that we have understood why we need array and how to initialize it. Now let's have a look how we can use an array inside a for-loop. Shown below is an example that has similar fruits array that we used in earlier examples.

string[] fruits = new string[] { "Apple", "Banana", "Grapes", "Mango", "Guava", "Watermelon", "Pomegranate" };
for (int i = 0; i < fruits.Length; i++)
{
    Console.WriteLine(fruits[i]);
}

//Output

//Apple
//Banana
//Grapes
//Mango
//Guava
//Watermelon
//Pomegranate

Here in this example, fruits array is iterated over and over again till we reach the last index of array which is 6 for Pomegranate.

That's all for now about arrays. Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)


Comments

Popular posts from this blog

Just Maths - Views, View Groups and Layouts

Create Android Apps - Getting Started