C array initialization

  • 2020-05-07 20:21:32
  • OfStack

Aside.NET has been around for a year, since C#- > ASP.NET- > WPF. Mainly to read e-books, write less code. Now go back to the previous contact, with the accumulation of knowledge and experience.

There are always surprises and shocks! The C# array is one of them, and I made it my blog garden debut.
The C# array is quite different from the other C languages, and has been widely misunderstood in the past. In particular, multidimensional arrays. Multidimensional arrays are a new concept compared to C. And the very beginning
I think of it as a special type of jagged array.

The initialization and access of 2 - dimensional arrays and simple interlaced arrays begin
 
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 do this very quickly for any array of dimensions, just foreach You cannot modify a variable.  

A jagged array can do much the same
 
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); 
} 
} 

The data stored in a multidimensional array can be replaced by a jagged array. A jagged array is a special array with high dimensions. And a jagged array is an array of arrays. And there's one very important property of arrays,
The array must be of the same type! This is important for understanding complex arrays.
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 had to initialize it like this with a bunch of new because a jagged array is an array of arrays, so we used to have 1 nested directly. But you need a lot of array types, and you can create an infinite number of array types.
 
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 jagged array.  
// bool[2][]  with boo[3][]  Is the same type, so we create the storage structure no 1 To an array of  

Next comes the most complex type. Mix the jagged array with the multidimensional array. If you can initialize the following array then you should understand it better!
bool [][,,][][,,][]Foo;
I choose a simple point as an example bool [][,][]Foo;
 
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()); 
// The results of   Mixed array type  
//system.boolean[][,][] 
//system.boolean[][,] 
//system.boolean[] 
//system.boolean 

 
// Define a jagged array: 1 Dimensional array storage ( 2 d int Array store ( 1 d int Array store ( 4 d int Array)))  
// The standard C# Definition description  array of( multi-array of( array of (nulti-array))) 
int[][,][][, , ,] arr = new int[10][,][][,,,]; 
// Initialize the  2 d int Array store ( 1 d int Array store ( 4 d int Array))  
arr[4] = new int[1, 2][][,,,]; 
// Initialize the  1 d int Array store ( 4 d int Array)  
arr[4][0, 1] = new int[3][, , ,]; 
// Initialize the  4 d int An array of  
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 inverted from what we declared. There should be no difference in understanding  

It should be clear by 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 1 on the array method. Especially Clear ();
 
Console.WriteLine(Foo[0][0,0][0]); 
// The output is Flase 
Array.Clear(Foo,0,1); 
Console.WriteLine(Foo[0][0, 0][0]); 
// A null reference exception is thrown here. because  bool[][,] The value of the type of null .  


Related articles: