Question: I have been learning. NET for a year, from C #-> ASP. NET-> WPF. It mainly focuses on reading e-books and writes less code. Now I will go back and learn what I have been in touch with before. With the accumulation of knowledge and experience.
There are always various surprises and surprises! The C # array is one of them. I use it as the debut of my blog.
C # arrays are different from other C-series languages, and there is a great deviation in understanding these arrays in the past. Especially for multi-dimensional arrays. Multi-dimensional arrays are a new concept compared with the C language. In the beginning
I think of it as a special type of the staggered array.
First, initialize and access the two-dimensional array and simple staggered array.Copy codeThe Code is as follows: int [,] nums = {
{1, 2, 3 },
{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 );
}
// You can quickly access any dimension array, but foreach cannot modify the variable.
The staggered array can also achieve similar content.Copy codeThe Code is as follows: 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 multi-dimensional arrays can be replaced by staggered arrays. An interleaved array is a special array with a high dimension. The staggered array is an array. And the array has a very important property,
The saved items in the array must be of the same type! This is important for understanding various complex arrays.
Complex staggered ArrayCopy codeThe Code is as follows: 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 a lot of new in this way. Because the staggered array is an array, we have been nested before. However, many array types are required, and countless array types can be created.Copy codeThe Code is as follows: Console. WriteLine ("staggered array type ");
Console. WriteLine (cells31 [0]. GetType ());
Console. WriteLine (cells31 [0] [0]. GetType ());
Console. WriteLine (cells31 [0] [0] [0]. GetType ());
// Staggered array type
// System. Boolean [] []
// System. Boolean []
// System. Boolean
// This is the type in the staggered array.
// Bool [2] [] and boo [3] [] are of the same type, so we create arrays with inconsistent storage structures.
Next is the most complex type. Mix the staggered array with the multi-dimensional array. If you can initialize the following array, you should have a thorough understanding!
Bool [] [,] [] [,] [] Foo;
I chose a simple point as the bool [] [,] [] Foo;Copy codeThe Code is as follows: bool [] [,] [] Foo = new bool [1] [,] []
{
New bool [2, 2] []
{
{
New bool [2] {false, true },
New bool [2] {false, true}
},
{
New bool [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 ());
// Mixed result array type
// System. boolean [] [,] []
// System. boolean [] [,]
// System. boolean []
// System. boolean
Copy codeThe Code is as follows: // defines the staggered array: one-dimensional array storage (two-dimensional int Array Storage (one-dimensional int Array Storage )))
// Standard C # definition description array of (multi-array of (nulti-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 ());
// System. Int32 [,] [] [,] []
// System. Int32 [,] [] [,]
// System. Int32 [,] []
// System. Int32 [,]
// C # The name generated by the compiler is inconsistent with what we declare. It should be okay to understand.
It should be clear now. I don't know if every programmer understands this, but it took me a lot of time to understand it.
Finally, consider the impact on the array method. Especially Clear ();Copy codeThe Code is as follows: Console. WriteLine (Foo [0] [0, 0] [0]);
// The output is Flase.
Array. Clear (Foo, 0, 1 );
Console. WriteLine (Foo [0] [0, 0] [0]);
// Null reference exception is thrown here. Because the value of the bool [] [,] type has changed to null.