Simple analysis of C # array initialization

Source: Internet
Author: User
There is always a variety of surprises, shock! C # array is one of them, I use it as my own blog garden debut.
C # Arrays are a lot different from other C-series languages, and there is a great deal of deviation from previous encounters. In particular, the understanding of multidimensional arrays. Multidimensional arrays are a new concept compared to the C language. And the beginning of the
I think of it as a special type of staggered array.

First, the initialization and access of a two-dimensional array with a simple interleaved array begins

int[,] nums={{A-i}, {1,2,0}}; for (int i = Nums. Getlowerbound (0); I <= nums. GetUpperBound (0); i++) {for (int j = Nums. Getlowerbound (1); J <= Nums. GetUpperBound (1); J + +) {Console.WriteLine (nums[i,j]); Console.WriteLine (Nums. GetValue (i,j)); }} foreach (var num in nums) {Console.WriteLine (num)}//For arrays of any dimension can be accessed so quickly, except that foreach cannot modify the variable.

and jagged arrays can achieve similar content.

Int[][] nums2 ={new int[]{1,2,3}, New int[]{1,2,0}}; for (int i = NUMS2. Getlowerbound (0); I <= nums2. GetUpperBound (0); i++) {for (int j = Nums2[i]. Getlowerbound (0); J <= Nums2[i]. GetUpperBound (0); J + +) {Console.WriteLine (nums2[i][j]);}} foreach (Var ia in nums2) {foreach (var i in IA) {Console.WriteLine (i);}}

Data stored in multidimensional arrays can be substituted with jagged arrays. A jagged array is a special array with a high dimension. A jagged array is an array of arrays. And the array has a very important property,
The deposit must be the same type in the array! This is important for understanding the various complex arrays.
A complex jagged array

bool[][][] cells31 = new bool[2][][] {new bool[2][] {new bool[] {false}, new bool[] {true}}, new bool[3][] {new bool[] {false}, new bool[] {true}, new bool[] {true}}};

We have to initialize this. There are a whole bunch of new because jagged arrays are arrays of arrays, so we've been nesting before. However, many array types are required, and countless array types can be created.

Console.WriteLine ("Staggered array type"); Console.WriteLine (Cells31[0]. GetType ()); Console.WriteLine (Cells31[0][0]. GetType ()); Console.WriteLine (Cells31[0][0][0]. GetType ()); Jagged array type//system.boolean[][]//system.boolean[]//system.boolean//This is the type of the jagged array. Bool[2][] is the same type as boo[3][], so we create an array of inconsistent storage structures

The next is the most complex type. Mixes a jagged array with a multidimensional array. If you can initialize the following array then you should understand the more thorough!
bool [][,,][][,,][]foo;
I choose a simple point as example bool [][,][]foo;

bool[][,][] Foo = new bool[1][,][] {new bool[2,2][] {{new bool[2] {false, true}, new Bool[2] {false, True}}, {new boo L[2] {false, true}, new Bool[2] {false, True}}}; Console.WriteLine ("Mixed array type"); Console.WriteLine (Foo.gettype ()); Console.WriteLine (Foo[0]. GetType ()); Console.WriteLine (foo[0][0,0]. GetType ()); Console.WriteLine (Foo[0][0, 0][0]. GetType ()); Result mixed array type//system.boolean[][,][]//system.boolean[][,]//system.boolean[]//system.boolean
Define a jagged array: one-dimensional array holds (two-dimensional int array storage (four-dimensional int array))//Standard C # definition Description array of Multi-array of (array of (Nulti-array)) int[][,][ [,,,] arr = new int[10][,][][,,,]; Initializes two-dimensional int array storage (one-dimensional array of int) (arr[4] = new int[1, 2][][,,,]; Initializes a one-dimensional array of int (array of four-dimensional int) arr[4][0, 1] = new int[3][,,,]; Initializes an array of four-dimensional int (arr[4][0) = 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). GetType ()); system.int32[,,,][][,][]//system.int32[,,,][][,]//system.int32[,,, []//system.int32[,,,]//c# The compiler generated a name that was inverted with what we declared. It doesn't have to be different to understand.

It should be clearer now. I don't know if every programmer understands this, but it took me a while to figure it out.
Finally, consider the effect of the logarithmic array method. In particular Clear ();

Console.WriteLine (Foo[0][0,0][0]); Output is flase array.clear (foo,0,1); Console.WriteLine (Foo[0][0, 0][0]); A null reference exception is thrown here. Because the value of the type of bool[][,] has become null.


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.