搜索
有爱,有技术,有你^_^)y
╱人◕‿‿◕人╲订下契约(注册新用户)

合作站点账号登陆

QQ登录

只需一步,快速开始

快捷导航
查看: 482|回复: 0
收起左侧

[普通教程] 【MSDNのC#系列教程】命令行参数教程

[复制链接]

签到天数: 2 天

连续签到: 2 天

[LV.1]初来乍到

48

主题

24

好友

1万

积分

第一章

积分
11403
发表于 2012-7-20 21:55:19 | 显示全部楼层 |阅读模式

╱人◕‿‿◕人╲定下契约

您需要 登录 才可以下载或查看,没有账号?╱人◕‿‿◕人╲订下契约(注册新用户)

x
此为MSDN的资料源地址:http://msdn.microsoft.com/zh-cn/library/ms123401

教程 下面的示例展示使用传递给应用程序的命令行参数的两种不同方法。

示例 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);
       }
   }
}



输出
使用如下所示的一些参数运行程序: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



願爾有生之日。   得見吾君臨天下
回复

使用道具 举报

本版积分规则

小黑屋|手机版|技术宅(Z站|基宅) ( 粤ICP备18082987号-1 )

GMT+8, 2025-5-2 09:47 , Processed in 0.054064 second(s), 10 queries , Redis On.

Copyright © 2018 技术宅社区

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表