C# Arrays (Array)
An array is a fixed-size sequential collection that stores elements of the same type. An array is a collection used to store data and is generally considered a collection of variables of the same type.
Declaring an array variable does not declare separate variables number0, number1, ..., number99; instead you declare a single variable like numbers and then use numbers[0], numbers[1], ..., numbers[99] to represent the individual variables. A specific element in an array is accessed by index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.
Declaring Arrays
To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
Where
- datatype Used to specify the type of the elements stored in the array.
- [ ] Specifies the rank (dimensions) of the array. The rank specifies the size of the array.
- arrayName Specifies the name of the array.
For example:
double[] balance;
Initializing Arrays
Declaring an array does not initialize it in memory. When initializing an array variable, you can assign values to the array.
An array is a reference type, so you need to use the new keyword to create an instance of the array.
For example:
double[] balance = new double[10];
Assigning values to an array
You can assign a value to an individual array element by using an index number, for example:
double[] balance = new double[10];
balance[0] = 4500.0;
You can assign values to an array at the same time you declare it, for example:
double[] balance = { 2340.0, 4523.69, 3421.0};
You can also create and initialize an array, for example:
int [] marks = new int[5] { 99, 98, 92, 97, 95};
In the above cases, you can also omit the size of the array, for example:
int [] marks = new int[] { 99, 98, 92, 97, 95};
You can also assign one array variable to another target array variable. In this case, the target and the source will point to the same memory location:
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
When you create an array, the C# compiler implicitly initializes each array element to a default value according to the array type. For example, all elements of an int array are initialized to 0.
Accessing Array Elements
Elements are accessed by the array name with an index. This is done by placing the element's index in square brackets after the array name. For example:
double salary = balance[9];
The following is an example using the three concepts mentioned above, namely declaration, assignment, and accessing the array:
Example
using
System
;
namespace
ArrayApplication
{
class
MyArray
{
static
void
Main
(
string
[
]
args
)
{
int
[
]
n
=
new
int
[
10
]
;
/* n is an array with 10 integers */
int
i,j
;
/* Initialize the elements in array n */
for
(
i
=
0
;
i
<
10
;
i
++
)
{
n
[
i
]
=
i
100
;
}
/* Output the value of each array element */
for
(
j
=
0
;
j
<
10
;
j
++
)
{
Console
.
WriteLine
(
"Element[{0}] = {1}"
, j, n
[
j
]
)
;
}
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Use foreach Loop
In the previous example, we used a for loop to access each array element; you can also use a ** foreach** statement to iterate over the array.
In the following example, we use foreach to iterate over an array:
Example
using
System
;
namespace
ArrayApplication
{
class
MyArray
{
static
void
Main
(
string
[
]
args
)
{
int
[
]
n
=
new
int
[
10
]
;
/* n is an array with 10 integers */
/* Initialize the elements in array n */
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
n
[
i
]
=
i
100
;
}
/* Output the value of each array element */
foreach
(
int
j
in
n
)
{
int
i
=
j
100
;
Console
.
WriteLine
(
"Element[{0}] = {1}"
, i, j
)
;
}
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
C# array details
In C#, arrays are very important, and more details need to be understood. The following lists some important array-related concepts that C# programmers must be clear about:
| Concept | Description |
|---|---|
| Multidimensional Arrays | C# supports multi-dimensional arrays. The simplest form of a multi-dimensional array is the two-dimensional array. |
| Jagged arrays | C# supports jagged arrays, that is, arrays of arrays. |
| Passing Arrays to Functions | You can pass a pointer to an array to a function by specifying the array name without an index. |
| Parameter arrays | This is typically used to pass an unknown number of arguments to a function. |
| The Array class | Defined in the System namespace, it is the base class of all arrays and provides various properties and methods for arrays. |