main方法中除字符串以外的参数 [英] Parameter other than string in main method

查看:72
本文介绍了main方法中除字符串以外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#控制台应用程序的主要方法中,我们通常使用string作为参数(

In the main method of a C# console application usually we use string as parameter (

static void Main(string[] args)





我们可以使用其他类型如Integer作为main方法的参数。除此之外我们可以使用多个参数是主要的方法吗?



我尝试了什么:



static void Main(int [] args){

}


)
Can we use other types such as Integer as parameter of main method. Besides can we use more than one parameter is main method?

What I have tried:

static void Main(int[] args){
}

推荐答案

您可以拥有与Main一样多的参数如你所愿,这就是为什么它被定义为 string [] 而不是 string



尽管所有参数都作为字符串传递给Main,但它们可以转换为其他类型。请参阅参考命令行参数(C#编程指南) [ ^ ]
You can have as many parameters to Main as you like, that is why it is defined as a string[] rather than string.

Although all of the arguments are passed into Main as strings, they can be converted to other types. See the reference Command-Line Arguments (C# Programming Guide)[^]


引用:

我们可以使用其他类型如Integer作为main方法的参数。另外我们可以使用多个参数是main方法吗?

Can we use other types such as Integer as parameter of main method. Besides can we use more than one parameter is main method?



你只能有字符串类型参数,你可以根据需要转换字符串。

分离参数需要一个简单的空间。


You can only have string type parameters, it is your task to convert strings as you need.
A simple space is needed to separate parameters.


你了解命令行的目的吗?以及为什么它以字符串数组表示给你的应用程序?



这是由历史原因完成的,因为字符串是传递数据的通用方法。这就是它,你不能改变它。



在实践中其他类型意味着使用字符串(或字符串)但尝试解析根据一些应用模式。



请参阅我的文章,我提供了这种易于使用的设施:基于枚举的命令行实用程序 [ ^ ]。



另一种选择,也是CodeProject的工作,在我的文章中引用。



-SA
Do you understand the purpose of command line; and why it's represented in array of strings, to your application?

It's done by historical reasons, because a string is a universal way to pass data. This is what it is, you cannot change it.

In practice "other types" means string using string (or strings) but trying to parse it according some application schema.

Please see my article where I present an easy-to-use facility of this kind: Enumeration-based Command Line Utility[^].

Another alternative, also a CodeProject work, is referenced in my article.

—SA


这篇关于main方法中除字符串以外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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