static void Main(string[] args)
{ int i;
if (args.Length > 0)
{
for (i = args[0].Length - 1; i >= 0; i--)
{
Console.Write(args[0][i]);
} Console.ReadKey();
}
else
{ Console.WriteLine("请输入要进行反向输出的字符串作为参数!"); }
Console.ReadLine();
}
}
1.貌似static void Main(string[] args)如果不传递参数好像看起来时1维数组?然后这里Console.Write(args[0][i]);看起来又好像是交错数组?args[i][j]含义是i个参数的第j个字符??2.如果不传递任何参数,我直接输出args[]结果是system.string[],可加了args[0]结果就错误了?
网上解释Main函数的参数,是执行时传过来的,如果执行的时候没传任何参数,那么args.Length就应该等于0,访问args[0]会出异常的:
System.IndexOutOfRangeException: 索引超出了数组界限。按F5运行的时候,一般情况下是不会传参数过来的。也就是说,args 是一个长度为0的字符串数组,访问args[0]是会出错的。长度为0的字符串数组不是输出null或者空格?到底什么时候数组输出null?什么时候数组输出空格
对于一个字符串也可以当字符数组来访问
string strTest = "test";
for(int i = 0; i < strTest.Length; i++)
{
Console.Writeln(strTest[i]);
}
string s = "12345";
Console.WriteLine(s[2]);
string[] x=null; Console.WriteLine("Main函数参数args的长度:" +x );
main(int argc, char** args)
如果是null,根本就无法读取长度属性的
Console.Write(args.Length);未将对象引用设置到对象的实例。
长度为0
string[] args = new string[0];
str1[2] = 'w';这个代码结果是什么?结论是回报错,这说明[]在这里只是一个索引器,而且是只读的索引器。对比:
int[] i = new int[]{ 1, 2, 3, 4 }
i[3] = 5;这个代码就没问题。