C# ARRAYS

C# ARRAYS

In C# sharp Arrays are used to store multiple data in a single  variable

To declare an array define variable type with [] square brackets.

In C#, an array is a collection of elements of the same type stored in contiguous memory locations.

In c#  there are different ways to create an array which you can see in below picture:


ACCESS THE ELEMENTS OF ARRAYS

To Access the elements of array we can access them through index number.


Array index starts with 0 so 0 is the first element and 1 is second element and so on.

CHANGE AN ELEMENT OF ARRAY

If we want to change the value of element in array we can change them through using index number.


At the time of changing the element we can add that type of element the kind of array we created,otherwise compile time error will come.

PROPERTIES OF ARRAYS

In C# Arrays have many properties here some are as follows:

Length - If we want to find length of our element we can use this property.

Rank - Gets the number of dimensions in array.

Long Length - Gets the total number of elements in the array as a 64-bit integer.

Apart from this many different properties are also there which we can use for our programming tasks to get its benefits and very helpful to make more concise our code.

METHODS OF ARRAYS

· In C# we have different methods of Arrays,here some are as follows:

Sort -  This method sort an alphabet in ascending order.

Min - This method returns smallest value.

Max - This method returns largest value.

Sum - Returns the sum of elements.

Min,Max,Sum can found in system.Linq namespace.

Aparts from this many methods are there which we can use in our programming task to achieve different results.

LOOPS THROUGH AN ARRAYS

We can perform for loop through the array elements and use the length property to know how many time our loop will run.

We can access an each array element one by one with the help of loop.


Here we can see above diagram how for loop work with array.

FOR EACH LOOP

For each loop exclusively work with elements of array and it is very easy to read and write compare to for loop,here you can see diagram of this.


In the following diagram for each loop presenting all the value of the element of array. you can see its easy to write and concise to read.

MULTIDIMENSIONAL ARRAY

In the above example we have seen the example of single dmensional array.This is seen more often use in our programming.

However if you want to store data in tabular format such as rows and columns we need to familiar with multidimensional array.

A multi dimensional array is basically arrays of array in rows and columns format.

Arrays has many number of dimensions.The most common are two dimensional arrays(2D).

To create two dimension arrays we have to insert comma when we adding curly braces after data type of array.Add each array with its own curly braces.Here is a small diagram to understand multi dimensional array.


For the accessment of 2D arrays we have to specify two indexes one for row and and one for column.

We can also change the elements of multidimensional array ,we can use loops also with them as well as properties and methods and many more things.

So in Summary,We have seen definition of array ,how to  change the elements in array ,how to access them,Array properties and its methods,Loops through an arrays,we also familiar with multidimensional array.

Arrays are fundamental in C# programming and are widely used for storing and manipulating collections of data.

Previous Next

Start Your Data Journey Today With MSAInfotech

Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!

We utilize data to transform ourselves, our clients, and the world.

Partnership with leading data platforms and certified talents

FAQ Robot

How Can We Help?

Captcha

MSA Infotech