C # Arrays-one-dimensional arrays

Source: Internet
Author: User
Declaring an array and assigning an initial value

Int[] arr = {1, 2, 3};

You can see that [] followed by the type name is the array, {} represents the list of initial values to be assigned, and each initial value is separated by commas.

Declaring an array

int[] arr = new INT[3]; Correct, declare an array of type int of length 3 int[3] arr2; Error, cannot specify array size int[] ARR3; The array size is not specified and cannot be used

If we do not know the value of the array in advance, we can declare the array as described above, of course, when declaring an array, you should know the type and size of the array, and the number in [] indicates that the length is not the upper bound of the subscript.

C # Arrays also support dynamically specifying

int len = 3;int[] arr = new Int[len];

assigning values to arrays

int[] arr = new int[3];arr[0] = 10; Assigns an int m = arr[0] to the first element; Take the value of the first element

Get array length

int[] arr = new Int[3];int len = arr. Length;int len2 = arr. GetLength (0)//This method can also get the array length, which indicates the length of the array to get the number of dimensions, starting from 0.

More convenient for applications under the. NET Framework 3.5

The C # 3.0 syntax is used under the. NET Framework 3.5, so it is more convenient to use an array, without specifying the length and directly assigning values.

string[] colors = new string[]{"#333", "#666", "#999", "#ccc", "#fff"};

Declares an array of colors and assigns five strings to it. The length of the array here is determined automatically by the number of braces.



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.