C# Arrays

An array is a collection of elements, of fixed size and type, for example, a collection of 10 elements of type int. It is a data structure to group elements i.e. storing more than one value in a single variable. In this lesson, we will learn how to work with arrays in Java Programming Language. Arrays in Java use index to access an element.

The following are the types of Arrays in C#:

  • One-Dimensional Arrays
  • Multi-Dimensional Arrays

Let us first workaround One-Dimensional Arrays in C#.

Declaration and Initialization

For declaring a one-dimensional array in C#, you need to specify the data type and the variable name like the following,

Here,

  • datatype is the type of the array, for example, int, float, etc.
  • [] is the size of the array
  • array_name is the name of the array

Therefore, an example to declare an array would be the following array of int:

Above, we saw how to declare an array variable, with our variable name, marks.

To create an array in C# and initialize it, use the new operator. For example, the following are the marks of 10 students:

Insert/ Assign array values

To assign values to an array in C#. Right now, we have space for 10 elements:

Add the index in the square brackets and insert the value to the specified element, like

Assign value to the 1st element:

Assign value to the 2nd element:

Assign value to the 3rd element:

.
.
.

Assign value to the 9th element:

Assign value to the 10th element:

The above can be directly written in a single line; declaration, and initialization together:

Or, do not mention the array size:

Or, even the new operator is not required to assign values at the declaration time:

Let us see what we have done till now,

Arrays in Csharp

First Array Program in C#

Let us now see an example to iterate and display the marks of 10 students in C#:

Output

We discussed one-dimensional arrays in C#. Let us now work around Multi-Dimensional Arrays in C# with examples.


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


C# - Strings
C# Multi-Dimensional Arrays
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading