An array is a continuous block of memory that is allocated for a set of items that share a common name. For example we define an array that contains a set of all the cities in India. A particular value is indicated by writing a number is called as index or subscript in brackets after the array name.Here in this blog we will discuss in detail about arrays in c# mainly about array declaration, creation and initialization.
Example
cities[5]; // here cities is an array name and 5 is the subscript
represents the city that is stored in the memory block 5. The entire set of values is known as the array and the individual element is known as elements. The ability to use a single name that represents a the complete collection and to refer to an item by specifying the item number enables the developers to develop more efficient code.For example, a loop can be used to read the entire collections in an array and print them on the console.We will see how to implement that later.
One-dimensional array in c#
A one dimensional array has only a there is only one subscript that is available.Example a[1], [2],a[3],………..a[n].
The subscript can begin with number 0. That is x[0].For example, if we want to represent a set of four number, let’s say (10,20,30,40), by an array variable number, then we must create the variable number as follows which is an array.
int [] number = new int [4];
the memory in the computer is allocated as follows, the reserves 4 spaces in memory starting from 0
The values to the array are assigned as follows:
number[0] = 10;
number[1] = 20;
number[2] = 30;
number[3] = 40;
Thus the memory looks as follows:
Creating an array in c#
Like any other variables, array must be declared and created in the computer memory before they are being used.Array creation involves the following three steps:
- declaring the array
- creating the memory locations
- putting values into the memory locations (or) initialization of arrays
Declaration of arrays in c#
The array in c# are declared as follows:
type[] arrayname;
examples
int [] age;
float[] marks;
int[ ] x,y;
Note: we do not enter the array size in the declaration
Creation of arrays
After declaring an array, we need to create it in the memory. In c# we can create arrays in memory using the new keyword and it is represented as below:
arrayname = new type[size];
example
number = new int [10]; // creates an array of size 10 of type integer
total = new float[15]; // creates an array of size 15 of type float
These line create the necessary memory locations for the arrays number and total.It is possible to combine the declaration and the creation together,which is done as follows:
int [] number = new int [10]; // declares and creates an array of size 10
Initialization of arrays
This is actually the final step and this process involves putting the values into the array that is created.This is done using the array subscript as shown below:
arrayname[subscript] = value;
example
number [0] = 10;
number [1] = 20;
c# always starts the array subscript from 0 and ends with one number less than the specified size.
Note : Trying to access an array element beyond it’s range will provide an error – ArrayIndexOutOfBounds Exception.
We can also initialize the arrays when they are declared itself like other variables.
type [] arrayname = { list of values };
example
int [] numbers = {10, 20 , 30};
These values are separated by commas and defined on both sides with curly braces, no size is given .The compiler allocates enough space for all the elements available in the list.
int[] number = { 10,20,30};
the above step is equivalent to
int[] number = new int [5] { 10,20,30};
It is possible to assign array object to another.
int [] m = {1,2,3};
int [] n;
n = m;
here both arrays will have same value.
This is all about the arrays in C# . How to create and declare the arrays in C#.