How C # arrays are declared

Source: Internet
Author: User
Tags type null
Five ways to declare a C # array

First, declare an uninitialized array reference, you can later make this reference to an array instance

Int[] Intarray;

Intarray = new INT[10];

Note: The reference to an array must be instantiated with the same or related type, the initial default value of the array, the value type 0, and the reference type NULL

Second, declare an array reference and make an initial representation of it, and the array reference is immediately given a new instance of the array.

int[] Intarray = new INT[10];

Declare an array, initialize the array references, and assign values to the array elements

int[] Intarray = new Int[3] {n-only};

Note: The initial list is separated by a comma (,), and the number in the list must match the length of the array.

Four, and the third way basically the same, just do not set the initial size of the array, determined by the array elements.

int[] Intarray = new int[] {n-only};

This is a simplified version of the fourth way, where the array type and the array size are inferred from the initial list.

Int[] Intarray = {n/a};

Multidimensional data

Multidimensional arrays are rectangular arrays, there are multiple dimensions and indexes, dimensions are separated by commas in [], "[,]", for example the most

Common two-dimensional arrays:

int[,] Intarray = new int[2,3] {{1,2,3},{4,5,6}};

Jagged arrays

A jagged array is considered an array of arrays, and each vector of a jagged array can have a different length.

You should first define the rows in the jagged array (the number of vectors), and then declare the number of elements per row

int[][] Intarray = new Int[2][]{new int[]{1,2,3},new int[] {1,2,3,4}}

Note the difference from a two-dimensional 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.