Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 6.0 C# "new console template" - how to read CLI arguments?

Now that .NET 6.0 is out, what appears to have be a radical update to the default CLI project template is the absence of the familiar boilerplate being reduced to the following:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

What is not clear (and I have been trying to find documentation thus far, to no avail) is how does one access the command-line arguments passed to the executable's entrypoint class?

like image 710
djtubig-malicex Avatar asked Nov 28 '21 10:11

djtubig-malicex


People also ask

Is .NET 6.0 the same as .NET Core?

Net Core. The latest version of . Net Core is . NET 6.0 and it was released on November 8, 2021.

Does C# 10 require .NET 6?

C# 10 is supported on .NET 6 SDK from the . NET downloads page. You can also download Visual Studio 2022, which includes the .

Is .NET Core 6 stable?

NET 6, which is the most recent LTS version. While support for . NET Core 3.1 ends in December 2022, support for . NET 6 will continue until November 2024.

What is new in net 6?

In this article, you will learn what is new in .NET 6.0. .NET 6 is the latest version of .NET that was released in Nov 2021. Not only is .NET 6 a much improved version of the framework compared to its predecessors, but it also introduces some of the coolest features we’ve seen in some of the most popular platforms and languages.

What is the latest version of NET Core?

In this article, I am going to explain what’s new in .NET 6.0, what .NET Core is, .Net Core features and benefits, .Net Core Versions, why we use .Net Core. The latest version of .Net Core is .NET 6.0 and it was released on November 8, 2021. What’s New in .NET 6.0? Microsoft was released .NET 6.0 on November 8, 2021.

What is the difference between NET 5 and net 6?

.NET 6 delivers the final parts of the .NET unification plan that started with .NET 5. . NET 6 unifies the SDK, base libraries, and runtime across mobile, desktop, IoT, and cloud apps. In addition to this unification, the .NET 6 ecosystem offers:

What version of Visual Studio do I need to use net 6?

If you want to use .NET 6, you will need to upgrade to Visual Studio 2022 (which is also now 64-bit ). . NET 6 is supported with the Visual Studio Code C# extension.


Video Answer


2 Answers

You can access the command line arguments from anywhere in your code using the Environment class.

In particular, you can use Environment.GetCommandLineArgs:

string name = Environment.GetCommandLineArgs()[1];
Console.WriteLine($"Hello, {name}!");

Note that the first element in the array contains the path of the executable and the arguments passed to the program start with the second element, i.e. at index 1.

like image 124
Codo Avatar answered Oct 24 '22 13:10

Codo


New project templates are using C# 9 feature called top-level statements.

For top-level statement file compiler will generate string[] args parameter (actually it generates the whole class containing Main method), so you can just use it (as previously was done with Main(string[] args)):

Console.WriteLine(args.Length);

More info about the generation patterns can be found in the docs.

like image 45
Guru Stron Avatar answered Oct 24 '22 13:10

Guru Stron