C# Top-level statement console template problem partly solved

📆

👤

The top-level statement console template problem started when .NET 6.0 was released back in November 8th, 2022. During its development time, there was a proposal from a C# developer who suggested using the top-level statements with implicit usings and nullable contexts in the console template which was unchanged from the very start of .NET going back to 2002. We have talked about the proposal in our past article.

Let’s restate our statement in different words. Developers who were excited about the top-level statements in console apps were happy that it made their programs look simpler, and one even stated that it was benefical for their teaching sessions because they didn’t have to say “just ignore the stuff in the curly braces”.

However, the majority of the developers didn’t like the feature because it also oversimplified things to the point that it made them angry, and more than 1000 developers protested against the new template and wanted the old template back.

So, Microsoft have apparently solved the problem by editing the template so it lets the developers choose whether to use the top-level statements or not. You may think that every developer were satisfied with the change as one developer stated.

Wrong!

Because developers that were dissatisfied about the top-level statements now had to deal with the implicit usings that, again, was criticized by them, the flame striked again, but, this time, it was a bit less dense than the principal problem. One developer discovered that the job wasn’t done properly as demonstrated here and here.

After you disable top-level statements in your new console project, you’ll be confronted with this:

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

…and this:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

…instead of the missed:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

As a result, many developers ranted about how implicit usings were so bad. Even we had to opt out of the implicit usings to get to work in all of our projects, and the most recent example of this is GRILO, which, during its first hours of development, we had to pull this stunt to really get started because we wanted to support .NET Framework 4.8 and 4.8.1.

Here are some pointers:

So when do we get an option to not use implicit using? We are still waiting to get the old template back.

mwwhited

This was very shocking when I first saw it…like ‘wtf happened?’ type shocking. Initially, I thought my VS instance was corrupt or something. Once I read up on it, I understand the desire to simplify…but this is a bit far. Its to far if C# wants to be a class based language. As many other posters above have stated, I think keeping the regular class structure is an absolute must and should be the default, not an option or the ‘old way’ of constructing a C# program.

ericHelinCulinaryflow

This is just confusing. If I want to just throw together a quick program, functions in that “magic implicit” main are local functions and so don’t support overloading. Was the existing template so egregiously bad that we needed to add a bunch of magic to make C# look more like a scripting language?

kylereedmsft

Here’s our statement:

Even though I saw the Don't use Top-level Statements checkbox, it doesn’t fully restore the old behavior of creating new console projects, which means that you’ll have to take out the ImplicitUsings thing and put the using System; statements at the very top of the file.

This is very annoying, because if you want this console project to be run in both .NET Framework and .NET 6.0, you’ll have to do the things I just said above.

I had to do it every time I make a demonstration of all my libraries or I make a future .NET console app which uses .NET SDK project style.

Aptivi

This not only angered the developers, but, according to StackOverflow’s developer survey, when the developers who were there were asked if they used C# in .NET 5.0 and 6.0, this came up:

In 2021, .NET Core / 5.0 developers at StackOverflow expressed their opinions regarding these frameworks, and the results came up:

In 2022, .NET Core / .NET 5.0 & 6.0 developers at StackOverflow expressed their opinions, and here are the results:

So, join our movement of #NoImplicitUsings on Twitter to express your dissatisfaction of implicit usings and, hopefully, get the template back!


Discover more from Aptivi

Subscribe to get the latest posts to your email.

Thoughts?

Subscribe to our newsletter?

Subscribe today to get new articles instantly delivered to you!

Not now

Design a site like this with WordPress.com
Get started