C# string[,]与string[][]的区别

对于这两者的区别:

1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作

  

static void Main(string[] args)
{ //声明变量
string[] str1 = new string[];
string[,] str2 = new string[, ];
string[][] str3=new string[][];
//string[][] str3 = new string[10][10]; //编译报错:无效的秩说明符: 应为“,”或“]” //赋值
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
str1[i] = i.ToString("") + j.ToString("");
//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 = ; i < ; i++)
{
System.Console.Write(str1[i]+" ");
}
System.Console.WriteLine();
System.Console.WriteLine("and:");
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
System.Console.Write(str1[i][j] + " ");
}
System.Console.WriteLine();
} //输出str[,]
System.Console.WriteLine();
System.Console.WriteLine("this is str[,]:");
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
System.Console.Write(str2[i,j]+" ");
}
System.Console.WriteLine();
} System.Console.ReadKey();
}

2.进阶:当然是在内存中占的内存不同;

  稍后善后

上一篇:String与StringBuffer的区别


下一篇:django之urlresolver