05 Feb 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,
1 2 3 |
datatype[] array_name; |
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:
1 2 3 |
int[] marks; |
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:
1 2 3 |
int[] marks = new int[10]; |
Insert/ Assign array values
To assign values to an array in C#. Right now, we have space for 10 elements:
1 2 3 |
int[] marks = new int[10]; |
Add the index in the square brackets and insert the value to the specified element, like
Assign value to the 1st element:
1 2 3 |
marks[0] = 95; |
Assign value to the 2nd element:
1 2 3 |
marks[1] = 90; |
Assign value to the 3rd element:
1 2 3 |
marks[2] = 99; |
.
.
.
Assign value to the 9th element:
1 2 3 |
marks[8] = 63; |
Assign value to the 10th element:
1 2 3 |
marks[9] = 91; |
The above can be directly written in a single line; declaration, and initialization together:
1 2 3 |
int [] marks = new int[10] { 95, 90, 99, 86, 80, 77, 92, 70, 63, 91}; |
Or, do not mention the array size:
1 2 3 |
int [] marks = new int[] { 95, 90, 99, 86, 80, 77, 92, 70, 63, 91}; |
Or, even the new operator is not required to assign values at the declaration time:
1 2 3 |
int [] marks = { 95, 90, 99, 86, 80, 77, 92, 70, 63, 91}; |
Let us see what we have done till now,
First Array Program in C#
Let us now see an example to iterate and display the marks of 10 students in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { // Array created int[] marks = new int[10]; // Assigning values to elements marks[0] = 95; marks[1] = 90; marks[2] = 99; marks[3] = 86; marks[4] = 80; marks[5] = 77; marks[6] = 92; marks[7] = 70; marks[8] = 63; marks[9] = 91; // Display the marks of all the students Console.WriteLine("Display the marks of 10 students..."); for (int i = 0; i < marks.Length; i++) { Console.WriteLine(marks[i]); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Display the marks of 10 students... 95 90 99 86 80 77 92 70 63 91 |
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
No Comments