C# array (Array)
Table of contents
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 an array is generally considered to be a collection of variables of the same type.
Declaring an array variable is not declaring number0, number1, ..., number99 individual variables, but declaring a variable like numbers, and then using numbers[0], numbers[1], ..., numbers[ 99] to represent individual variables. A specific element in the 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.
declare array
To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
in,
- datatype is used to specify the type of elements to be stored in the array.
- [ ] specifies the rank (dimension) of the array. The rank specifies the size of the array.
- arrayName specifies the name of the array.
For example:
double[] balance;
Initialize the array
Declaring an array does not initialize the array in memory. When initializing an array variable, you can assign to the array.
Arrays are a reference type, so you need to use the new keyword to create an instance of an array.
For example:
double[] balance = new double[10];
assign to array
You can assign 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 arrays while declaring them, for example:
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 size of the array, 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, the destination 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.
access array elements
Elements are accessed by indexed array name. This is done by placing the index of the element in square brackets after the array name. For example:
double salary = balance[9];
Here is an example using the three concepts mentioned above, namely declaration, assignment, and access to 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 */ 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 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 a 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 through 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 */
/* Initialize 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 need to know more details. Here are some important concepts related to arrays that C# programmers must be aware of:
concept | describe |
---|---|
Multidimensional Arrays | C# supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array. |
jagged array | C# supports jagged arrays, that is, arrays of arrays. |
pass the array to the function | You can pass a pointer to an array to a function by specifying the array name without an index. |
parameter array | This is often used to pass an unknown number of arguments 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. |