An array is a sequential collection of fixed sizes that stores elements of the same type. An array is a collection used to store data, and an array is generally thought of as a collection of variables of the same type.
Declaring an array variable is not a declaration
number0
、
number1
、…、
number99
individual variables, but declare one like
numbers
such a variable, and then use the
numbers[0]
、
numbers[1]
、…、
numbers[99]
to represent individual variables. A specified element in the array is accessed by index.
All arrays are made up of consecutive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to thelast element. To declare an array in C #, you can use the following syntax: Among them For example: Declaring an array does not initialize the array in memory. When you initialize an array variable, you can assign values to the array. Array is a reference type, so you need to use the For example: You can assign values to a single array element by using index numbers, suchas: You can assign values to an array while declaring it, such as: You can also create and initialize an array, such as: In the above cases, you can also omit the size of the array, such as: You can also assign an array variable to another target array variable. In this case, the destination and source point to the same memory location: 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 the The element is accessed through the indexed array name. This is achieved by placing the index of the element in square brackets after the array name. For example: Here is an example that uses the three concepts mentioned above, namely, declare, assign, and access arrays: When the above code is compiled and executed, it produces the following results: In the previous example, we used a When the above code is compiled and executed, it produces the following results: In C #, arrays are very important and you need to know more details. Here are some important array-related concepts that C # programmers must be awareof: Concept Description Multidimensional array C # supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array. Staggered array C # supports staggered arrays, that is, arrays of arrays. Pass an array to a function You can pass a pointer to the array to the function by specifying the name of the array without an index. Parameter array This is usually used to pass an unknown number of arguments to a function. Array class Defined in the System namespace, is the base class for all arrays and provides a variety of properties and methods for arrays. 1.25.1. Declare array #
datatype[] arrayName;
datatype
use to specify the type of element stored in the array.
[
]
specifies the rank (dimension) of the array. Rank specifies the sizeof the array.
arrayName
specifies the name of the array.double[] balance;
1.25.2. Initialize array #
new
keyword to createan instance of the array.double[] balance = new double[10];
1.25.3. Assign values to an array #
double[] balance = new double[10];
balance[0] = 4500.0;
double[] balance = { 2340.0, 4523.69, 3421.0};
int [] marks = new int[5] { 99, 98, 92, 97, 95};
int [] marks = new int[] { 99, 98, 92, 97, 95};
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
int
array are initialized to
0
. 1.25.4. Access array elements #
double salary = balance[9];
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 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();
}
}
}
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
1.25.5. Use foreach Cycle #
for
loop to access each array element. You can also use a
foreach
statement to traverse 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();
}
}
}
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
1.25.6. C # array details #