Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

Just switched to VS2022, created new project and see this:

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

Where is all other stuff? Why is that by default now?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Visual Studio 2022 with .NET 6 uses a new template when creating a C# console app. The new template reduces the amount of boilerplate code necessary to write a simple C# program. This change will certainly benefit beginning programmers and those who are new to C#.

New style:

  • Statements in Program.cs that appear outside of any function are automatically placed in Main().
  • Function declarations are moved outside Main() and made static.
  • A number of using statements are implicitly added for common namespaces like System, System.IO, System.Linq, etc.

Example:

// New style

Console.WriteLine("Code in Main()");
Test();

void Test()
{
    Console.WriteLine("Test");
}

is roughly equivalent to:

// Old style

using System;

namespace MyApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Code in Main()");
            Test();
        }

        static void Test()
        {
            Console.WriteLine("Test");
        }
    }
}

There is nothing stopping you from using the old style. You can copy and paste the old-style code above into Program.cs, and it will run just fine.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!