News
.NET 6 Preview 7: New Features Done, but Templates with Top-Level Statements Stir Debate
"That's a wrap on another season of .NET previews," said Microsoft's Richard Lander in announcing .NET 6 Preview 7.
He said new features are basically all done, with the dev team now focusing on polishing things up as it tackles two release candidates that might be used in production before general availability in November. That shouldn't be too much of a problem because .NET 6 has been powering key Microsoft properties -- Bing.com and the .NET web site -- since Preview 1. Now, other organizations might follow suit.
"We're in talks with various teams (Microsoft and otherwise) about going into production with the .NET 6 RCs," said Lander, program manager on the .NET team, in an Aug. 10 blog post. He invited organizations interested in adopting .NET 6 for production to contact Microsoft for details and guidance, because "We're always happy to talk to early adopters." To get things ready for production workloads, the dev team is now focused on the final touches to bring everything up to a high, uniform quality level, following the Themes of .NET plan:
Lander said this last preview is especially important as team members have worked hard to get new features added before the RC polishing stage while balancing priorities like:
- Add a lot of features each release to provide new value.
- New releases are high-quality.
- Ship according to our annual November schedule.
As far as new features in Preview 7, some received polish and some were "large it-took-the-whole-release features."
For example, .NET SDK templates -- after a period of relative neglect -- have been updated to incorporate the latest features of the company's flagship C# programming language, now at version 9. Several templates were updated to leverage the latest language idioms. For example, C# 9's top-level statements allow for programs without a Main method in certain projects. Code often found in Main is moved to the Program.cs file in Console apps, for example. Developers can use top-level statements to write C# statements without having to explicity create a namespace, class and a static Main method as an entry point to a program. Global using
statements and compiler behind-the-scenes machinations fill in the "missing" code.
Thus, a Console template app can be reduced from the old way:
using System;
namespace Company.ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
to the new way:
Console.WriteLine("Hello, World!");
Microsoft says top-level statements can be used to build code as you learn along with creating programs without Main methods. The latter post says: "Top-level statements let you write simple programs for small utilities such as Azure Functions and GitHub Actions. They also make it simpler for new C# programmers to get started learning and writing code."
However, that "learning" aspect apparently provided the impression that some template changes using top-level statements are geared toward new developers or amateurs rather than experienced pros.
For example, here are a couple of questions posed in comments on Lander's blog, and his responses:
-
Q: "I get that C# is trying to compete with Python and JavaScript for attention of younger devs, but can you please don't remove the expert templates?"
Lander: "It's not that. These templates are rooted in a model from (at least) 40 years ago. They don't pivot on expertise. It's more like old school / new school. Sure, we do believe that these templates will make it easier to learn C# for folks familiar with Python or JavaScript or completely new to programming. Perhaps that's half the reason for the change. The other half is that the current templates don't carry their weight and are also confusing. I've had to look up the syntax for AsyncMain multiple times (even though it is trivial). Top-level statements erases that whole problem. That's a virtue for exerts and novices alike."
-
Q: "So from what I gather from the links you provided, this is only for 'simple programs and new programmers.' Is there a config option for this available or do you force this on all of us?"
Lander: "What leads you to that conclusion? The top-level statements model enable almost all the functionality of the 'classic' one. It absolutely extends beyond simple programs and new programmers. Here's a good example a non-simple program: https://github.com/dotnet/iot/blob/main/src/devices/Pn532/samples/Program.cs. What more would you expect to do beyond what this program demonstrates?"
Other comments about top-level statements included:
- I do not understand why its a good thing that the default templates will now produce such 'useless' default files. Useless because every real application will have to copy the missing things back. How much console applications do you write which do not need args? Where do you put the documentation for the functions used in Program.cs?
-
I tend to agree with others that I don't agree with changing the existing templates. New templates should be added instead.
I have been teaching C# to beginners for years and never have I had a single student struggle with the basic structure of a new console application. Most students have already learned either Java or C++ and therefore understand entry points, Main and the equivalent of using. They understand functions and so that is where we stay for the first 2 weeks. They get it.
It is clear the top-level statements, global using, etc are designed to cut down on the code but the reality is that it is still there. The compiler is just auto-generating it. If something isn't right then it becomes harder to diagnose. If you need to customize it then it just looks odd compared to the full version. For use in sample code and quick start guides I'm totally fine with the concept but for any real program I think it is taking a step back from good coding practices.
Beyond top-level statements, other language features used or enabled by default in SDK-included project templates include:
Speaking of that last item, nullable reference types, developers can now inspect them with new Reflection APIs that expose top-level nullability information from Reflection, helping them express whether a a given reference type can be built and determine the nullability nature of parameters and return values for a given method.
On the libraries front, System.Text.Json has been improved in multiple ways, including the new ability to control the serialization order of properties. Also, its serializer now exposes notifications as part of (de)serialization operations.
Along with a whole bunch of other tweaks to libraries and much more new functionality, early .NET 7 features are available in preview, such as allowing static abstract
members to be declared in interfaces. "For example, it is now possible to implement algebraic generic methods," Lander said about the new ability for developers to define static abstract
methods -- including operators -- in interfaces. "For some folks, this feature will be the absolute standout improvement we're delivering this year. It is perhaps the most important new type system capability since Span<T>." It is so important that generic math got its own blog post.
As far as those other library improvements, here are links for more information:
Lander had previously indicated Preview 6 was the last chance to influence the release, but in closing yesterday's announcement he said: "We continue to want and rely on your feedback. We will focus the rest of .NET 6 on regressions (functional and performance) and bugs found in the new features. Functional improvements in most cases will need to wait for .NET 7. Please share any and all feedback you have and we'll be happy to categorize it."
About the Author
David Ramel is an editor and writer at Converge 360.