C# Array
An array is a fixed-size collection of elements of the same type stored sequentially. Arrays are used to store collections of data, often considered as a collection of variables of the same type.
Declaring an array variable does not declare individual variables like number0, number1, ..., number99, but rather declares a single variable like numbers, and then uses numbers[0], numbers[1], ..., numbers[99] to represent individual variables. A specific element in an array is accessed by its 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 an Array
To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
Where:
- datatype specifies the type of elements stored in the array.
- [ ] specifies the rank (dimension) of the array. The rank determines the array's size.
- arrayName specifies the name of the array.
For example:
double[] balance;
Initializing an Array
Declaring an array does not initialize it in memory. When initializing an array variable, you can assign values to the array.
Arrays are reference types, 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 values to individual array elements using their index numbers, such as:
double[] balance = new double[10];
balance[0] = 4500.0;
You can also assign values to an array while declaring it, like:
double[] balance = { 2340.0, 4523.69, 3421.0 };
You can also create and initialize an array, like:
int[] marks = new int[5] { 99, 98, 92, 97, 95 };
In the above case, you can also omit the array size, like:
int[] marks = new int[] { 99, 98, 92, 97, 95 };
You can also assign an array variable to another target array variable. In this case, both the target and 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 based on the array type. For example, all elements of an int array will be initialized to 0.
Accessing Array Elements
Elements are accessed using the array name with the index in brackets. For example:
double salary = balance[9];
Below is an example that uses the three concepts mentioned above: declaration, assignment, and access of arrays:
Example
using System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int[] n = new int[10]; /* n is an array of 10 integers */
int i, j;
/* Initialize array n's elements */
for (i = 0; i < 10; i++)
{
n[i] = i + 100;
}
/* Output each array element's value */
for (j = 0; j < 10; j++)
{
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}
When the above code 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
Using foreach Loop
In the previous example, we used a for loop to access each array element. You can also use a foreach statement to traverse through the array.
Example
using System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int[] n = new int[10]; /* n is an array of 10 integers */
/* Initialize array n's elements */
for (int i = 0; i < 10; i++)
{
n[i] = i + 100;
}
/* Output each array element's value using foreach */
foreach (int j in n)
{
int i = j - 100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the same result as before.
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 above code 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 there are more details to understand. Here are some important concepts related to arrays that C# programmers must be aware of:
Concept | Description |
---|---|
Multi-dimensional Arrays | C# supports multi-dimensional arrays. The simplest form of a multi-dimensional array is a two-dimensional array. |
Jagged Arrays | C# supports jagged arrays, which are 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 generally used for passing an unknown number of parameters to a function. |
Array Class | Defined in the System namespace, it is the base class for all arrays and provides various properties and methods for arrays. |
```