10

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?

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@LasseV.Karlsen Re: Let's clean up the comment thread then :) I'll delete my comments because they make no sense any more. Hm, I should have been more precise with my edit summary then (eg., added more information following Lasse V. Karlsen's suggestion in the comments) for appropriate credit, but I hope this comment captures its gist :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.