Home>Article>Backend Development> How to assign values to arrays in C#

How to assign values to arrays in C#

高洛峰
高洛峰 Original
2016-12-16 14:51:41 3598browse

How to assign values to arrays in C#

After declaring an array, you can fill it with values immediately. This is done by using a comma-separated list of data items within a pair of curly braces. Listing 2-30 declares an array of strings and then specifies the names of nine different programming languages within a pair of braces.

Assign values while declaring the array

string[] languages = { "C#", "COBOL", "Java",
"C++", "Visual Basic", "Pascal",
"Fortran", "Lisp" , "J#"};

In this comma-separated list, the first item becomes the first element of the array, the second item becomes the second element, and so on. We use curly braces to define an array literal.
The assignment syntax in Listing 2-30 is only available when declaring and assigning in the same statement. If the assignment is made after declaration, you need to use the new keyword and the corresponding data type, as shown in code listing 2-31.

Code List 2-31: Array assignment after declaration

string[] languages;
languages = new string[]{"C#", "COBOL", "Java",
"C++", "Visual Basic", "Pascal",
"Fortran", "Lisp", "J#" };

C# also supports the use of the new keyword as part of the declaration statement, so it allows assignments and declarations like the code in Listing 2-32.

Code List 2-32 Use new for array assignment while declaring

string[] languages = new string[]{
"C#", "COBOL", "Java",
"C++", "Visual Basic", "Pascal",
"Fortran", "Lisp", "J#"};

Using the new keyword tells the "runtime" to allocate memory for the data type. It indicates the "runtime" instantiated data type - in this case an array.

Any time you use the new keyword as part of an array assignment, you can also specify the size of the array within square brackets. Code Listing 2-33 demonstrates this syntax.

Code List 2-33 Use the new keyword for declaration and assignment

string[] languages = new string[9]{
"C#", "COBOL", "Java",
"C++", "Visual Basic" , "Pascal",
"Fortran", "Lisp", "J#"};


In the initialization statement, the size of the array and the number of elements contained in the curly braces must match. Alternatively, you can allocate an array without specifying its initial value, as shown in Listing 2-34.

Code Listing 2-34 Allocating an array without specifying an initial value

string[] languages = new string[9];

Allocating an array without specifying an initial value will still initialize each element. The "runtime" will initialize each element to their default value as follows:

Reference types (such as string) are initialized to null;

Numeric types are initialized to zero;

bool is initialized to false;

char initialized to

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn