Loading, please wait...

MCQ

What are Arrays in C# | C# Tutorials

Dec 18, 2020 C#, CSharp, Shibam Dhar, 1547 Views
C# provides numerus built-n classes to store and manipulate data. One of such classes is the Array Class.

What are Arrays in C# | C# Tutorials

C# provides numerous built-n classes to store and manipulate data. One of such classes is the Array Class. An array is a data structure that is used to store a collection of data

The declaration of the array is usually like this:

int[] myArray

in the above code, int specifies the data type to be inserted inside the array, which is an integer.

We can initiate the array with the help of a new keyword since arrays can be objects.

Example:

int[] myArray = new int[40]

double[] anotherArray = new double[6]

There are different things that are possible to carry out with arrays, in C#.

Assigning values to the array:

myArray[2]=23;

in the above code, it means that the value 23 will be stored as an integer in the 2nd index of the myArray array.

Providing initial values:

While declaring an array we can provide some initial values to it, such as a few numbers or string values.

string[] names = new string[2] {“sam”, “mike”};

In the above code, the name array is created of string type with sam and mine as initial values in it.

Omitting size declaration:

We can omit the size declaration of the array, and it will take spaces according to the values we give to it.

double[] hello = new double[]{1,2,4,5,6,7};

Omitting new keyword:

To make the array neat in the program we can even omit the new keyword, and write the array directly.

int[] myArray = {1,2,3,4,5,6,7};

Using loops in array

Arrays might get long or too much filled with data, so to traverse through each of them we can use looping. To loop, there are several methods, one of them is for each loop. Each loop provides a simple, clean way to iterate through the elements of a collection or an array of items. To print each element in an array we can not use for each, instead, we use for method.

The syntax for:

for(initialization; condition; final expression)

The syntax for each:

foreach (Data_Type variable_name in Collection_or_array_Object_name) {   //body of foreach loop }

Here Data_Type is a data-type of the variable and variable_name is the variable that will iterate the loop condition

NOTE: To break the loop we can use a break at any point to come out of the loop.

Multidimensional Array

As the name suggests, an array with more than one dimension. Multi-dimensional arrays are also called a rectangular array. 

The initializing of more than 1-dimensional arrays is quite easy.

2-dimensional array

string [,] myArray;

A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.

int [,] a = new int [2,4] {

   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */

   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */

};

To access it, we have to pass the index value in both coordinates.

int val = a[1,3];

3-dimensional array

int [ , , ] myArray;

Jagged Array

This is an array whose elements are arrays. These arrays can be of different sizes. These are arrays of arrays.

int[][] jaggedArray = new int[2][];

The above code  is a declaration of a single-dimensional array that has two elements, each of which is a single-dimensional array of integers:

You can initialize the elements like this:

jaggedArray[0] = new int[5];

jaggedArray[1] = new int[4];

MCQ

Related Article