How arrays are assigned values in C #

Source: Internet
Author: User
How arrays are assigned values in C #

After you declare an array, you can immediately populate it with values. method is to use a comma-delimited list of data items in a pair of curly braces. Listing 2-30 declares a string array, and then specifies the names of 9 different programming languages in a pair of curly braces.

Assigning values while declaring an array

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

In this comma-delimited 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 for code listing 2-30 is available only if you declare and assign a value in the same statement. If you assign a value after declaring it, you need to use the New keyword and the corresponding data type, as shown in Listing 2-31.

Array assignment after Code listing 2-31 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 a declaration statement, so it allows assignments and declarations like code listing 2-32.

Array assignment with new at the same time as code listing 2-32 declaration

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 that the runtime instantiates the data type-this example is an array.

Whenever you use the New keyword as part of an array assignment, you can specify the size of the array within square brackets at the same time. Code Listing 2-33 demonstrates this syntax.

Code listing 2-33 declaration and assignment using the New keyword

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. In addition, you can assign an array, but do not specify its initial value, as shown in Listing 2-34.

Code Listing 2-34 allocates an array, but does not specify an initial value

String[] languages = new string[9];

Assigning an array but not specifying an initial value will still initialize each element. The runtime initializes each element to their default value, as follows:

The reference type (such as String) is initialized to null;

The numeric type is initialized to zero;

bool initialized to false;

Char is initialized to.

As a result, you do not have to assign each element of the array individually before you use it.

In C # 2.0, you can use the default () operator to determine the defaults for a data type. Default () Gets a data type as a parameter. For example, default (int) returns 0, and default (char) returns.

Because the array size is not part of the variable declaration, you can specify the array size at run time. For example, code listing 2-35 creates an array based on the size specified by the user in the Console.ReadLine () call.

Code Listing 2-35 defines the array size at run time

String[] grocerylist;
System.Console.Write ("How many items on the list?");
int size = Int. Parse (System.Console.ReadLine ());
Grocerylist = new String[size];
// ...

C # takes a similar approach to working with multidimensional arrays. To separate the size on each dimension with a comma. Code Listing 2-36 Initializes a tic-tac-toe chessboard without moves.

Code Listing 2-36 declares a two-dimensional array

int[,] cells = int[3,3];

You can also initialize a tic-tac-toe checkerboard to a specific piece layout, as in Listing 2-37.

Initializes a two-dimensional array of integers

int[,] cells = {
{1, 0, 2},
{1, 2, 0},
{1, 2, 1}
};

The pattern used for initialization is an array of elements with 3 int[] types, each of which has the same size. In this case, the size is 3. Note that the dimensions of each int[] element must be exactly the same. In other words, a declaration like code listing 2-38 is not valid.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.