C# string[,]与string[][]的区别
对于这两者的区别:
1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作
static void Main(string[] args) { //声明变量 string[] str1 = new string[10]; string[,] str2 = new string[10, 10]; string[][] str3=new string[10][]; //string[][] str3 = new string[10][10]; //编译报错:无效的秩说明符: 应为“,”或“]” //赋值 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { str1[i] = i.ToString("00") + j.ToString("00"); //str1[i][j] = i.ToString("00") + j.ToString("00"); //编译不过:无法对属性或索引器“string.this[int]”赋值 -- 它是只读的 str2[i, j] = i.ToString() + j.ToString(); } } //输出str[][] System.Console.WriteLine("this is str[]:"); for (int i = 0; i < 10; i++) { System.Console.Write(str1[i]+" "); } System.Console.WriteLine(); System.Console.WriteLine("and:"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 4; j++) { System.Console.Write(str1[i][j] + " "); } System.Console.WriteLine(); } //输出str[,] System.Console.WriteLine(); System.Console.WriteLine("this is str[,]:"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.Console.Write(str2[i,j]+" "); } System.Console.WriteLine(); } System.Console.ReadKey(); }
2.进阶:当然是在内存中占的内存不同;
稍后善后