New in .NET 10.0 [16]: Passing parameters in File-based Apps

The File-based Apps introduced in .NET 10.0 can also receive command-line parameters.

listen Print view
Sign with inscription "C#"

(Image: Pincasso/Shutterstock)

1 min. read
By
  • Dr. Holger Schwichtenberg

Directly translating and starting C# files is called File-based Apps by Microsoft. You can pass additional parameters to it via the command line.

The Dotnet Doctor – Holger Schwichtenberg
Der Dotnet-Doktor – Holger Schwichtenberg

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with consulting and training services as well as software development, drawing on the expertise of 53 renowned experts. Thanks to his appearances at numerous national and international conferences, as well as more than 90 specialist books and over 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

Parameters can be passed both in conjunction with class Program and the Main() method, as well as when using Top-Level Statements, because the compiler also provides the args variable for them.

The following code example shows an extended Hello World with parameters:

using System; // nicht notwendig, Standardnamensräume sind immer dabei, da <ImplicitUsings>enable</ImplicitUsings> gesetzt ist

var conf = args.FirstOrDefault() ?? "diesem Vortrag";
Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
Console.WriteLine($"Hallo liebe Teilnehmerinnen und Teilnehmer bei \e[4;33;5m{conf}\e[0m!");
Console.WriteLine($"Kompilierungsmodus: {(System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported ? "JIT" : "AOT")}");

Videos by heise

Calling the File-based App with a parameter in Windows Terminal (Fig. 1)

(mho)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.