Home > Backend Development > C#.Net Tutorial > How to declare C# array

How to declare C# array

高洛峰
Release: 2016-12-16 14:48:04
Original
1305 people have browsed it

Five ways to declare arrays in C#

1. Declare an uninitialized array reference. This reference can be initialized into an array instance later

int[] intArray;

intArray = new int[10];

Note: The reference of the array must be instantiated with the same or related type. The array is initialized with the default value, the value type is 0, and the reference type is null

2. Declare an array reference and initialize it. The array reference is immediately assigned to a new instance of the array.

int[] intArray = new int[10];

3. Declare an array, initialize the array reference, and assign values ​​to the array elements

int[] intArray = new int[3] {1,2 ,3};

Note: The initialization list is separated by commas (,), and the number in the list must be consistent with the length of the array.

Fourth, it is basically the same as the third method, except that the initial size of the array is not set and is determined by the array elements.

int[] intArray = new int[] {1,2,3};

5. This is a simplified version of the fourth method, in which the array type and array size are inferred based on the initialization list of.

int[] intArray = {1,2,3};

Multidimensional data

Multidimensional array is a rectangular array with multiple dimensions and indexes. The dimensions are separated by commas in [], "[,] ”, for example, the most common two-dimensional array:

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

interleaved Array

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

First define the rows (number of vectors) in the interleaved array, and then declare the number of elements in each row

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

Pay attention to the difference with two-dimensional arrays


For more articles related to the declaration method of C# arrays, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template