1、二维数组初始化
public double[,] data = new double[2, 100];
Ps:
开发的时候,有试过创建长度为10000000的数组(局部变量),报错-->“如果要创建数组,请确保大小正确”
但创建为全局变量的时候不报错。
//据说在函数内部的变量都是在栈里,栈的大小根据编译器有不同,一般是1M
2、二维数组清空
Array.Clear(data, 0, data.Length); //清空索引0开始的数组内容
3、数组复制
(1)二维数组 Clone
newData = (double[,])data.Clone();
(2)一维数组 CopyTo
int[] copy = new int[array .Length];
array.CopyTo(copy, 0);
CopyTo方法用作将源数组全部拷贝到目标数组中,可以指定目标数组的起始索引,但需确保目标数组能容纳下源数组,CopyTo可用来合并多个数组。
Ps:
CopyTo()只适用于一维数组,虽然写的时候不会报错
4、获取二维数组长度
int row = Arr.GetLength(0); //行数
int col = Arr.GetLength(1); //列数
5、截取数组
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] tt = data.Skip(1).Take(data.Length).ToArray();
take的参数如果大于数组的长度,则截取到数组结束
6、数组批量赋相同值
int[] array = Enumerable.Repeat(0, 1000).ToArray();
//0是值,1000是数目,也就是数组的长度
7、数组作为构造函数传参时,报TypeInitializationExeption错误
通俗点的:https://blog.csdn.net/u012576247/article/details/39435437?utm_source=blogxgwz6
如果这个参数是静态变量的话,在执行构造函数的时候就会产生静态成员的初始化错误。
最快解决方法就是不要用静态变量作为传参。