Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda