Csharp 简明教程
C
一个数组存储类型相同的元素的固定大小顺序集合。数组用于存储数据集合,但通常将数组视作同一类型的变量集合更有益,这些变量存储在连续的存储器位置。
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
不要声明诸如 number0、number1、… 和 number99 这样的独立变量,而是声明一个阵列变量,如 numbers,并使用 numbers[0]、numbers[1] 和…,numbers[99] 来表示独立变量。阵列中的特定元素通过索引进行访问。
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
所有阵列都包含连续的内存位置。最低地址对应于第一个元素,而最高地址对应于最后一个元素。
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Declaring Arrays
要在 C# 中声明一个数组,你可以使用以下语法 −
To declare an array in C#, you can use the following syntax −
datatype[] arrayName;
其中,
where,
-
datatype is used to specify the type of elements in the array.
-
[ ] specifies the rank of the array. The rank specifies the size of the array.
-
arrayName specifies the name of the array.
例如,
For example,
double[] balance;
Initializing an Array
声明数组不会在内存中初始化数组。当数组变量被初始化时,您可以为数组赋值。
Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.
数组是一种引用类型,因此您需要使用 new 关键字创建一个数组实例。例如:
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 values to individual array elements, by using the index number, like −
double[] balance = new double[10];
balance[0] = 4500.0;
您可以在声明时为数组赋值,如下所示 −
You can assign values to the array at the time of declaration, as shown −
double[] balance = { 2340.0, 4523.69, 3421.0};
您还可以创建并初始化一个数组,如下所示 −
You can also create and initialize an array, as shown −
int [] marks = new int[5] { 99, 98, 92, 97, 95};
您还可以省略数组的大小,如下所示 −
You may also omit the size of the array, as shown −
int [] marks = new int[] { 99, 98, 92, 97, 95};
您可以将一个数组变量复制到另一个目标数组变量中。在这种情况下,目标和源都指向相同的内存位置 −
You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location −
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
当您创建数组时,C#编译器会根据数组类型隐式地将每个数组元素初始化为一个默认值。例如,对于 int 数组,所有元素都初始化为 0。
When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements are initialized to 0.
Accessing Array Elements
通过对数组名称进行索引来访问元素。这是通过在数组名称后面方括号中放置元素索引来完成的。例如:
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example,
double salary = balance[9];
以下示例演示了上述概念的声明、赋值和访问数组 −
The following example, demonstrates the above-mentioned concepts declaration, assignment, and accessing arrays −
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 elements of array n */
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 the foreach Loop
在前面的示例中,我们使用 for 循环访问每个数组元素。您还可以使用 foreach 语句遍历数组。
In the previous example, we used a for loop for accessing each array element. You can also use a foreach statement to iterate through an array.
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [] n = new int[10]; /* n is an array of 10 integers */
/* initialize elements of array n */
for ( int i = 0; i < 10; i++ ) {
n[i] = i + 100;
}
/* output each array element's value */
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
以下是与数组相关的几个重要概念,C# 程序员应该清楚它们 −
There are following few important concepts related to array which should be clear to a C# programmer −
Sr.No. |
Concept & Description |
1 |
Multi-dimensional arraysC# supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. |
2 |
Jagged arraysC# supports multidimensional arrays, which are arrays of arrays. |
3 |
Passing arrays to functionsYou can pass to the function a pointer to an array by specifying the array’s name without an index. |
4 |
Param arraysThis is used for passing unknown number of parameters to a function. |
5 |
The Array ClassDefined in System namespace, it is the base class to all arrays, and provides various properties and methods for working with arrays. |