Home  >  Article  >  Backend Development  >  A brief analysis of C# array initialization

A brief analysis of C# array initialization

高洛峰
高洛峰Original
2016-12-16 14:42:421534browse

There are always various surprises and shocks! C# array is one of them, and I regard it as my debut blog.
C# arrays are very different from other C series languages. There was a big difference in understanding when I was exposed to them in the past. Especially the understanding of multi-dimensional arrays. Multidimensional arrays are a new concept compared to C language. At first, I regarded it as a special type of jagged array.

First focus on the initialization and access of two-dimensional arrays and simple interleaved arrays

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); 
} 
//对任意维度的数组,都可以这样快速访问,只是foreach不能修改变量。

And interleaved arrays can also 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); 
} 
}

The data stored in multi-dimensional arrays can be replaced by interleaved arrays. A jagged array is a special array with high dimensions. And a jagged array is an array of arrays. And arrays have a very important property,

The items stored in the array must be of the same type! This is important for understanding various 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 must initialize like this. There is a lot of new. Because the jagged array is an array of arrays, we have always nested it before. But a lot of array types are needed, and countless array types can be created.

Console.WriteLine("交错数组类型"); 
Console.WriteLine(cells31[0].GetType()); 
Console.WriteLine(cells31[0][0].GetType()); 
Console.WriteLine(cells31[0][0][0].GetType()); 
//交错数组类型 
//System.Boolean[][] 
//System.Boolean[] 
//System.Boolean 
//这是交错数组里面的类型。 
// bool[2][] 与boo[3][] 是相同的类型,所以我们创建存储结构不一致的数组

The next is the most complex type. Mix jagged arrays with multidimensional arrays. If you can initialize the array below, you should understand it more thoroughly!

bool [][,,][][,,][]Foo;
I chose 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("混合数组类型"); 
Console.WriteLine(Foo.GetType()); 
Console.WriteLine(Foo[0].GetType()); 
Console.WriteLine(Foo[0][0,0].GetType()); 
Console.WriteLine(Foo[0][0, 0][0].GetType()); 
//结果 混合数组类型 
//system.boolean[][,][] 
//system.boolean[][,] 
//system.boolean[] 
//system.boolean
//定义交错数组:一维数组存放(二维int数组存放(一维int数组存放(四维int数组))) 
//标准的C#定义描述 array of( multi-array of( array of (nulti-array))) 
int[][,][][, , ,] arr = new int[10][,][][,,,]; 
//初始化 二维int数组存放(一维int数组存放(四维int数组)) 
arr[4] = new int[1, 2][][,,,]; 
//初始化 一维int数组存放(四维int数组) 
arr[4][0, 1] = new int[3][, , ,]; 
//初始化 四维int数组 
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#编译器生成的名字与我们声明的是倒着的。理解起来应该也没差异吧

It should be clearer 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 array methods. Especially Clear();

Console.WriteLine(Foo[0][0,0][0]); 
//输出为Flase 
Array.Clear(Foo,0,1); 
Console.WriteLine(Foo[0][0, 0][0]); 
//这里会引发空引用异常。因为 bool[][,]的类型的值已经变为null。


For more articles related to the brief analysis of C# array initialization, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn