Declaration of C # arrays

Source: Internet
Author: User
In C #, the declaration of an array is an instantiation of the arrays.

Declaration of a one or one-D array

1. Integer array

The following declares a one-dimensional array with 5 integer elements:

int[] array = new INT[5]; The above array contains elements from array[0] to array[4] that have a total of 5 integers. The new operator is used to create an array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero.

2. Array of strings

string[] Stringarray = new STRING[10]; The above array contains elements from stringarray[0] to stringarray[9] totaling 10 characters.

Declaration of a two or two-D array

The syntax format for declaring a two-dimensional array is as follows:

data type [,] array name =new data type [second dimension size, first dimension]; For example, the following declares a two-dimensional array of 5 rows and 3 columns:

int[,] array = new int[5,3];5 refers to the second-dimension size in a two-dimensional array, and the 3 column refers to the first-dimensional size in a two-dimensional array.

Declaration of a three or three-D array

The syntax format for declaring a three-dimensional array is as follows:

Data type [,,] array name =new data type [third dimension size, second dimension size, first dimension size]; For example:

int[,,] array = new int[4,5,3];

Iv. Declaration of jagged arrays

The following declares a one-dimensional array of 5 elements, each of which is a one-dimensional array of integers:

int[][] Jiaoarray = new int[5][]; iterate through jagged arrays:

for (int x = 0; x < jiaoarray.length; × x + +)
{
JIAOARRAY[X] = new INT[4];
}

V. Mixed use of rectangular arrays and jagged arrays

The following code declares a one-dimensional array of three-dimensional arrays of two-dimensional arrays of type int:

int[][,,][,] array;



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.