How to force Visual Studio 2022 create console projects with namespaces and classes (like in old good days)?

Click the link. It redirects to https://docs.microsoft.com/nl-nl/dotnet/core/tutorials/top-level-templates. It has a paragraph stating:

If you want to use the old templates, see the Use the old program style section.

That section mentions that this is the new default. To circumvent it, create a .NET 5-targeting application, and modify your project file:

-   <TargetFramework>net5.0</TargetFramework>
+   <TargetFramework>net6.0</TargetFramework>

A workaround I guess would be to create a custom project template.


You can access the args through a special variable with that name in a top level statement class file:

if (args.Length > 0)
{
    foreach (var arg in args)
    {
        Console.WriteLine($"Argument={arg}");
    }
}
else
{
    Console.WriteLine("No arguments");
}

Similarly, you just return an int to set the exit code:

string? s = Console.ReadLine();

int returnValue = int.Parse(s ?? "-1");
return returnValue;

See:

  • https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements

As to why, there has been a push to clean up the source files from needless whitespace, masses of curly braces, long lists of imports at the top of each file and the explicit namespace declaration, where most everyone syncs the namespaces with the assembly name and solution folder anyway.

It's been a thorn in the eye of many that simple things in c# need 10s of lines of code where they are a single line in node or python or ruby. It's just not productive. Same for Razor templates and Razor files. You just need an IDE to do she right thing. With these changes it should become much easier to be productive from the GitHub, even if you're not using Visual Studio.