Why should I define the cross array of C # In reverse mode?

Source: Internet
Author: User
Tags array definition

To be honest, the staggered array is not frequently used (although it is useful). Every time I use it, I have to hesitate to fill in the brackets for the array initialization length ...... Today, I looked at the staggered array definition compiled by myself ...... The array of C # is defined in this way (the multi-dimensional array is similar, but a comma is added in the box ):

// C # definition of one-dimensional array

Type[] Array= New Type[10];

The "type" must be any specific type, interface, and array. The so-called staggered array is actually an array, but the element type of this array is still an array, so it becomes a staggered array.

 

Then we define a simple interleaved array: an element is an interleaved array of one-dimensional int arrays. Obviously, this interleaved array is a one-dimensional array and its array type is a one-dimensional array, the type of this one-dimensional array is int. In this case, the above "type" is int []. Therefore, this array should be defined as follows:

//CodeCompilation failed

Int[] [] Array= New Int[] [10];

However, vs prompts a compilation error:

Obviously, vs tells us that the array size must be filled in the first box!

 

The above example is not enough to illustrate the problem, because two [] [] cannot be distinguished. Let's define another two-dimensional staggered array. The array type is int [], then let's take a look at its GetType return value:

// Correct staggered array definition: note that the type is [,] [] instead of [] [,]

Int[,] [] Array= New Int[1,1] [];

Console.Writeline (Array.GetType ());

// Output: system. int32 [] [,]

 

Result ...... Output: system. int32 [] [,]! [] Before [,], it fully complies with the above inference.

 

Perform a more complex test:

// Defines the staggered array: one-dimensional array storage (two-dimensional int Array Storage (one-dimensional int Array Storage (four-dimensional int array )))

Int[] [,] [] [,] Arr= New Int[10] [,] [] [,];

 

// Initialize two-dimensional int Array Storage (one-dimensional int Array Storage (four-dimensional int array ))

Arr [4]= New Int[1,2] [] [,];

 

// Initialize one-dimensional int Array Storage (four-dimensional int array)

Arr [4] [0,1]= New Int[3] [,];

 

// Initialize the four-dimensional int Array

Arr [4] [0,1] [2]= New Int[1,2,3,4];

 

Console.Writeline (ARR.GetType ());

Console.Writeline (ARR [4].GetType ());

Console.Writeline (ARR [4] [0,1].GetType ());

Console.Writeline (ARR [4] [0,1] [2].GetType ());

 

Conclusion:

GetType () Definition

========================================================== ================================

System. int32 [,] [] [,] [] [N] [,] [] [,] one-dimensional int staggered Array Storage (two-dimensional int Array Storage (one-dimensional int Array Storage (four-dimensional int array )))

System. int32 [,] [] [,] [N, N] [] [,] Two-Dimensional int staggered Array Storage (one-dimensional int Array Storage (four-dimensional int array ))

System. int32 [,] [] [N] [,] One-dimensional int staggered Array Storage (four-dimensional int array)

System. int32 [,] [N, N] four-dimensional int Array

 

 

Obviously, items in the C # staggered array definition are inverted orders of items of the array type.

if it is defined in the normal order, C # does not need the new concept of the so-called staggered array, because the so-called staggered array is the array, which can be directly defined. But C # Finally deliberately reversed the type definition method and called this method "staggered array". Why? Just to keep the initialization size of each array definition in the first box?

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.