什么是"字串[] args"在Main类? [英] What is "string[] args" in Main class for?

查看:95
本文介绍了什么是"字串[] args"在Main类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中的主类具有的String [] args参数。



什么是,和它在哪里使用呢?


< DIV CLASS =h2_lin>解决方案

从MSDN上< C#编程指南/ A>:



Main方法的参数是一个表示命令行参数的字符串数组



所以,如果我有一个计划(MyApp.exe将)是这样的:

 类节目
{
静态无效的主要(字串[] args)
{
的foreach(在args VAR ARG)
{
Console.WriteLine(ARG);
}
}
}

这是我开始喜欢这个命令行:

  MyApp.exe中的Arg1参数3  

Main方法将被传递包含三个字符串数组:则Arg1,Arg2所得,ARG3



如果您需要传递包含空格的参数,然后把它包在引号。例如:

  MyApp.exe的精氨酸1,2精氨酸精氨酸3 

的命令行参数通常得到时,你需要在运行时将信息传递给你的应用程序中使用。例如,如果你正在写一个程序,将文件从一个位置一个文件到另一个你很可能会通过两个位置作为命令行参数。例如:

  Copy.exe C:\file1.txt C:\file2.txt  


In C# the Main class has string[] args parameter.

What is that for and where does it get used?

解决方案

From the C# programming guide on MSDN:

The parameter of the Main method is a String array that represents the command-line arguments

So, if I had a program (MyApp.exe) like this:

class Program
{
  static void Main(string[] args)
  {
    foreach (var arg in args)
    {
      Console.WriteLine(arg);
    }
  }
}

That I started at the command line like this:

MyApp.exe Arg1 Arg2 Arg3

The Main method would be passed an array that contained three strings: "Arg1", "Arg2", "Arg3".

If you need to pass an argument that contains a space then wrap it in quotes. For example:

MyApp.exe "Arg 1" "Arg 2" "Arg 3"

Command line arguments commonly get used when you need to pass information to your application at runtime. For example if you were writing a program that copies a file from one location to another you would probably pass the two locations as command line arguments. For example:

Copy.exe C:\file1.txt C:\file2.txt

这篇关于什么是&QUOT;字串[] args&QUOT;在Main类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆