C# 程序员参考--命令行参数教程(转)

本教程展示如何访问命令行以及访问命令行参数数组的两种方法。
教程
    下面的示例展示使用传递给应用程序的命令行参数的两种不同方法。

示例 1
    本示例演示如何输出命令行参数。
    // cmdline1.cs
    // arguments: A B C
    using System;

    public class CommandLine
    {
       public static void Main(string[] args)
       {
          // The Length property is used to obtain the length of the array.
          // Notice that Length is a read-only property:
          Console.WriteLine("Number of command line parameters = {0}",
             args.Length);
          for(int i = 0; i < args.Length; i++)
          {
             Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
          }
       }
    }
输出
    使用如下所示的一些参数运行程序:cmdline1 A B C。
     输出将为:
    Number of command line parameters = 3
    Arg[0] = [A]
    Arg[1] = [B]
    Arg[2] = [C]
示例 2
    循环访问数组的另一种方法是使用 foreach 语句,如本示例所示。foreach 语句可用于循环访问数组或“.NET Framework”集合类。它提供了一种简单的方法来循环访问集合。
    // cmdline2.cs
    // arguments: John Paul Mary
    using System;

    public class CommandLine2
    {
       public static void Main(string[] args)
       {
          Console.WriteLine("Number of command line parameters = {0}",
             args.Length);
          foreach(string s in args)
          {
             Console.WriteLine(s);
          }
        }
     }
输出
    使用如下所示的一些参数运行程序:cmdline2 John Paul Mary。

    输出将为:
    Number of command line parameters = 3
    John
    Paul
    Mary

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924161/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924161/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值