C # one-dimensional Array initialization detailed

Source: Internet
Author: User

The understanding of C # one-dimensional arrays before we look at what is an array? An array is a collection of several variables of the same type that can be referenced with the same name. Arrays are composed of contiguous storage units, the lowest address corresponds to the first element of the array, the highest address corresponds to the last element, and the array can be one-dimensional or multidimensional.

A one-dimensional array is essentially a table-one-dimensional array of similar data

You can declare an array of 5 integers as shown in the following example:

int[] array = new INT[5];

This array contains elements from array[0] to array[4. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero.

You can declare an array of stored string elements in the same way. For example:

string[] stringArray = new string[6];

Initialization of a C # one-dimensional array

You can initialize an array when it is declared, in which case the level specifier is not required because the level specifier is already provided by the number of elements in the initialization list. For example:

int[] array1 = new Int[5] {1, 3, 5, 7, 9};

You can initialize an array of strings in the same way. The following declares a string array in which each array element is initialized with the name of the day:

string[] weekDays =
new string[] { "Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat" };

If you initialize an array when it is declared, you can use the following shortcuts:

int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon",
 "Tue", "Wed", "Thu", "Fri", "Sat" };

You can declare an array variable without initializing it, but you must use the new operator when assigning an array to this variable. For example:

int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 };   // OK
//array3 = {1, 3, 5, 7, 9};   // Error

The concepts and contents of the initialization of a C # one-dimensional array are introduced here to help you understand and learn the initialization of a C # one-dimensional array.

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.