简体   繁体   中英

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.

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:

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?

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 :

Starting in C# 9, you can omit the Main method, and write C# statements as if they were in the Main method [...] For information about how to write application code with an implicit entry point method, see Top-level statements .

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. The args variable is never null but its Length is zero if no command-line arguments were provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM