C#进阶教程(二)
2007-09-28 16:12
253 查看
下面的代码显示了我们如何打印多个参数:
000: // CommandLine/cmdline1.cs
001: using System;
002:
003: public class CommandLine
004: {
005: public static void Main(string[] args)
006: {
007: Console.WriteLine( "Number of command line parameters = {0}", args.Length);
008: for(int i = 0; i < args.Length; i++)
009: {
010: Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
011: }
012: }
013: }
如果你的输入为:
cmdline1 A B C
那么它的输出为:
Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]
哈哈,看出了其中的秘密了吧!是的{0}是一个标记,它告诉系统,这里留下了给第0个参数用,在程序中,这第0个参数就是i.因此,打印出来的就是Arg[0],面不是Arg[{0}]了,哈哈! 例二向大家展示了foreach这个语句的用法,其实在ASP中,这个经常用到:
000: // CommandLine/cmdline2.cs
001: using System;
002:
003: public class CommandLine2
004: {
005: public static void Main(string[] args)
006: {
007: Console.WriteLine( "Number of command line parameters = {0}", args.Length);
008: foreach(string s in args)
009: {
010: Console.WriteLine(s);
011: }
012: }
013: }
很显然,args是一个数组,而且是字符型的.foreach的意思就是把其中的所有元素都循环完.
运行得出:
>cmdline2 John Paul Mary Number of command line parameters = 3 John Paul Mary
000: // CommandLine/cmdline1.cs
001: using System;
002:
003: public class CommandLine
004: {
005: public static void Main(string[] args)
006: {
007: Console.WriteLine( "Number of command line parameters = {0}", args.Length);
008: for(int i = 0; i < args.Length; i++)
009: {
010: Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
011: }
012: }
013: }
如果你的输入为:
cmdline1 A B C
那么它的输出为:
Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]
哈哈,看出了其中的秘密了吧!是的{0}是一个标记,它告诉系统,这里留下了给第0个参数用,在程序中,这第0个参数就是i.因此,打印出来的就是Arg[0],面不是Arg[{0}]了,哈哈! 例二向大家展示了foreach这个语句的用法,其实在ASP中,这个经常用到:
000: // CommandLine/cmdline2.cs
001: using System;
002:
003: public class CommandLine2
004: {
005: public static void Main(string[] args)
006: {
007: Console.WriteLine( "Number of command line parameters = {0}", args.Length);
008: foreach(string s in args)
009: {
010: Console.WriteLine(s);
011: }
012: }
013: }
很显然,args是一个数组,而且是字符型的.foreach的意思就是把其中的所有元素都循环完.
运行得出:
>cmdline2 John Paul Mary Number of command line parameters = 3 John Paul Mary
相关文章推荐
- SUNWEN教程之----C#进阶(一)_C#教程
- SUNWEN教程之----C#进阶(七)_C#教程
- C#基础教程-委托进阶,将方法绑定到委托
- C#进阶教程(四)
- C#进阶教程(十一)
- SUNWEN教程之----C#进阶(八)_C#教程
- SUNWEN教程之----C#进阶(十一)_C#教程
- SUNWEN教程之----C#进阶(五)_C#教程
- SUNWEN教程之----C#进阶(四)_C#教程
- SUNWEN教程之----C#进阶2
- C#进阶教程(五)
- SUNWEN教程之----C#进阶(二)_C#教程
- SUNWEN教程之----C#进阶(六)_C#教程
- C#进阶教程 C#中的属性
- SUNWEN教程之----C#进阶9
- SUNWEN教程之----C#进阶5
- SUNWEN教程之----C#进阶(九)_C#教程
- SUNWEN教程之----C#进阶10
- C#进阶教程(六)
- SUNWEN教程之----C#进阶(三)_C#教程