What are Arrays in C# (Sharp)? Part 1

What are Arrays in C# (Sharp)? Part 1

C# has  various classes that are built-in and they can store and manipulate data.
An example of such a class is the Array class.
In order to store a collection of data the data structure that is used in C# is known as array. It can be described as a collection of variables of the same type.
Let’s assume a situation where you need to store 250 numbers. One option is to declare 250 different variables but a smarter solution is to declare an array that stores 250 elements.
To declare an array, specify its element types with square brackets:

int[ ] yourArray;

This is an example of declaring array of integers.
Arrays are objects, hence we need to instantiate them with the new keyword:

int[ ] yourArray = new int[15];

Note – Remember the Syntax it will be discussed in later topics as well.

Once an array is created,  individual elements can be assigned a value by using the index number:

int[ ] yourArray = new int[15];
yourArray[0] = 52;

This will assign the value 52 to the first element of the array.
Arrays in C# are zero-indexed.

Note – The first member in an array has index 0, the second member has index 1, and so on.

Social Share

Leave a Reply

Your email address will not be published. Required fields are marked *