Common array encoding errors in C #

Source: Internet
Author: User

Excerpt from C # essence (Fourth edition,P55)

Common errors Error description Corrected code
int numbers[] The brackets used to declare the array are placed after the data type, not after the variable identifier Int[] numbers;

Int[] numbers;

NUMBERS={42,84,168};

If you assign a value to an array after the declaration, you need to use the New keyword and optionally specify a data type .

Int[] numbers;

Numbers=new int[]{42,84,168};

INT[3] numbers={42,84,168}; You cannot specify an array size in a variable declaration Int[] numbers={42,84,168};
Int[] Numbers=new int[]; Unless you provide an array literal, you must specify the size of the array at initialization time. Int[] Number=new int[3];
Int[] Numbers=new int[3]{} The array size is specified as 3, but there are no elements in the array literal. The size of the array must match the number of elements in the array literal.

Int[] Numbers=

New int[3]{42,84,168};

Int[] Numbers=new int[3];

Console.WriteLine (Numbers[3]);

The array index starts at zero. Therefore, the index of the last item is 1 smaller than the array length. Note that this is a run-time error, not a compile-time error.

Int[] Numbers=new int[3];

Console.WriteLine (Numbers[2]);

Int[] Numbers=new int[3];

Numbers[numbrs. length]=42;

Same as the previous error: you need to subtract one from length to access the last element. Note that this is a run-time error, not a compile-time error.

Int[] Numbers=new int[3];

Numbers[numbers. length-1]=42;

Int[] numbers;

Console.WriteLine (Numbers[0]);

The numbers array has not been instantiated, so it is temporarily inaccessible.

Int[] numbers={42,84};

Console.WriteLine (Numbers[0]);

int[,] numbers=

{{42}. {84,42}};

The structure of multidimensional arrays must be consistent int[,] numbers={{42,168},{84,42}};

Int[][] Numbers=

{{42,84}.

{84,42}};

A jagged array requires an array of arrays to be instantiated. Int[][] numbers={new int[]{42,84},new int[]{84,42}};

Common array encoding errors in C #

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.