[英]C# args without a main method?
I am new to the C# world but have seen other programming languages give access to command-line arguments from places like main
function/method, sys.argv
, etc.我是 C# 世界的新手,但已经看到其他编程语言可以从main
函数/方法、 sys.argv
等地方访问命令行参数。
It was unusual to see the following statement placed globally in the boilerplate ASP.NET Core Web App MVC (Version 6.0) Program.cs
file of Visual Studio:这是不寻常地看到样板ASP.NET核心Web App的MVC(版本6.0)在全球范围放在下面的语句Program.cs
Visual Studio中的文件:
var builder = WebApplication.CreateBuilder(args);
Hovering over that parameter inside my IDE does seem to suggest it is indeed string[] args
, but where is the entry point?将鼠标悬停在我的 IDE 中的该参数上似乎确实表明它确实是string[] args
,但入口点在哪里?
I hope someone brings in further interesting details, but as a minimum, a bit of searching yielded the following result (short answer — Top-level Statements ) from Microsoft Docs :我希望有人带来更多有趣的细节,但至少,一些搜索从Microsoft Docs 中产生了以下结果(简短回答 -顶级声明):
Starting in C# 9, you can omit the
Main
method, and write C# statements as if they were in theMain
method [...] For information about how to write application code with an implicit entry point method, see Top-level statements .从 C# 9 开始,您可以省略Main
方法,并像在Main
方法中一样编写 C# 语句 [...] 有关如何使用隐式入口点方法编写应用程序代码的信息,请参阅顶级语句。
The page covering top-level statements makes it more clear:覆盖顶级语句的页面使其更加清晰:
args
Top-level statements can reference the
args
variable to access any command-line arguments that were entered.顶级语句可以引用args
变量来访问输入的任何命令行参数。 Theargs
variable is never null but itsLength
is zero if no command-line arguments were provided.args
变量永远不会为空,但如果未提供命令行参数,则其Length
为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.