What to Expect in .NET 6?

Microsoft is all set to unveil the latest iteration of its powerhouse, .NET 6. The release date is set for November 9th, and the development community is on the edge of its seats. Up until now, we have seen seven preview releases, each delivering features like server/cloud, desktop, IoT, and mobile, and two more are still anticipated.

Given its roots, .NET has, is, and always will be one of the most feature-rich frameworks ever built, and .NET 6 shows no signs of letting down its fans. With the seven preview releases, we now have a decent set of new features that we can discuss while making an educated assumption of what more .NET 6 will bring to the table. So without further ado, let’s get to it.

1. .NET 6 New Project Templates

The essence of .NET 6’s new project templates is based on the updates that C# 10 carries. These new project templates are clean and simple, and highly helpful in creating console applications. Upon installing the .NET SDK, the user receives over a dozen templates that can be deployed to build projects and files usable in console applications, unit test scenarios config files and class libraries.

In .NET 6, when you begin creating console applications, you will get this line of code-

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

This immediately simplifies the coding process where you meet all your requirements with a single hyper-effective line of code.

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

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

So how did this become possible? Well, here are three new features that have enhanced the simplicity of console coding.

  • Top-level statements
  • This feature has already appeared in C# 9, in .NET 6, the default templates of the framework use this feature. With this feature, developers no longer have to include the MAIN method into the console app project they are working on, thus radically minimizing the amount of code to be written.

  • Introduction of the ‘Global’ keyword
  • As you know, C# code starts with a list of ‘usings’ for successful implementation. However, it has been noticed that sometimes these usings are redundant. C# 10, the language used for .NET 6, has introduced the ‘Global” keyword. With Global, developers can now define global usings for the entire project in a separate file that contains imports like usings .cs. This greatly simplifies the project; in technical terms- “eliminating vertical waste.”

  • Implicit usings directives
  • This feature of .NET 6 pushes the compiler to begin importing a set of usings according to the type of the project being developed. This means that for each project type, .NET 6 defines an implicit set of global uses that don’t need to be physically stated in the code file.

    E.g., Implicit Global usings for a console application is as follows-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Json;

    Pro Tip- Once you use a project template to enable these features, you get your cake and eat it too. Now you have new code that starts with these features, but your existing code isn’t impacted during an upgrade.

    2. Package Validation Tooling

    For developers, package validation tooling eases out the development process while being exceptionally useful. Presented in .NET 6 preview 5, package validation tooling is the perfect in-house tool to ensure the proper formulation of multi-targeted packages, and this tool can be easily shipped to all users.

    With package validation tooling, developers can now validate the consistency of their packages against previous versions, older versions of the framework, and even against runtime. This example shows how to include this tooling into your project-

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

       <Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.5.21302.8" />
        <PropertyGroup>
         <TargetFrameworks>net5;net6.0</TargetFrameworks>
        </PropertyGroup>

    </Project>

    As mentioned earlier, this tooling offers three forms of validation, each of which is detailed in the next section.

    2.1 Package Validation – Compatible Frameworks

    Package validation tooling can cache errors during pack time. In the example below, PV tooling offers the ability to verify that code compiled for one framework runs on another framework as well. An example of how to check for compatibility across frameworks is as follows-

    public class TestClass
    {
        #if NET6_0_OR_GREATER
        public void DoSomething(ReadOnlySpan input)
        {
           //Some .NET 6 Operations
       }
       #else
       public void DoSomething(string input)
        {
           //Some .NET 5 Operations
      }
      #endif
    }

    Looking to Hire top 1% of talent
    in .NET Development?

    2.2 Package Validation – Version of the Package

    It is very common for earlier versions of the package to display unanticipated behavior even though everything is perfect from a code perspective.

    When a developer changes the public API of the package, assembled compilations based on an older version of the library you happen to be using can no longer call the API. With .NET 6’s package validation tooling, you can detect this issue and wish it a cherry goodbye.

    This is an example of how the project can be defined-

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

      <Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.5.21302.8" />
         <PropertyGroup>
        <TargetFrameworks>net6.0</TargetFrameworks>
        <PackageVersion>2.0.0</PackageVersion>
      <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion>
       </PropertyGroup>

    </Project>

    The code of TestClass should be as follows

    public class TestClass
    {
       public void DoSomething(string input)
       {
         //Some .NET 6 Operations
        }
      }

    You can achieve results with .Net 6’s PackageValidationBaselinePath :

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

      <Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.5.21302.8" />
        <PropertyGroup>
        <TargetFrameworks>net6.0</TargetFrameworks>
        <PackageVersion>2.0.0</PackageVersion>
        <PackageValidationBaselinePath>"C:\Users\n.zivkovic\source\repos\TestPackage\TestPackage\bin\DebugV1\net6.0\”
    </PackageValidationBaselinePath>
         </PropertyGroup>

    </Project>

    2.3 Package Validation – Runtime

    If you wish for your code to be compatible with Unix or Windows, here is how the code should look-

    public class TestClass
    {
       #if Unix
       public void DoSomething(string input, bool input2)
       {
         // Unix specific stuff
        }

         public void DoSomething(string input)
        {
          // Unix specific stuff
         }
        #else
        public void DoSomething(string input)
        {
             // Windows specific stuff
           }
           #endif
    }

    However, this code contains an error. Here, we have not used DoSomething(string input) with just one parameter for Unix. Package Validation tooling will tell us exactly this. The code should look like this:

    public class TestClass
    {
       #if Unix
       public void DoSomething(string input, bool input2)
       {
          // Unix specific stuff
         }
         public void DoSomething(string input)
         {
           // Unix specific stuff
          }
         #else
          public void DoSomething(string input)
         {
           // Windows specific stuff
         }
         #endif
    }

    3. .NET 6 Multi-platform App UI (MAUI)

    Microsoft has been talking about a ‘Big Unification’ ever since the release of .NET 5. They function with a vision that desires a single file of .NET code to work seamlessly across platforms like Windows, Linux, macOS, iOS, Android, tvOS, watchOS, and WebAssembly.

    .NET 6 carries the legacy of its predecessor ahead with a single .NET runtime and framework with uniform runtime behavior and can be used everywhere.

    Dot NET 6 Multi-platform App UI

    .NET 6 takes yet another step towards utopian coding by including Xamarin alongside iOS and Android. In 2020, Microsoft announced MAUI (.NET Multi-platform App UI). MAUI is, in essence, a highly functional UI toolkit built on Xamarin. Choosing Xamarin to build MAUI was mostly a community-based decision.

    “Xamarin has been a part of .NET for ages and now ships as a core workload. Xamarin shares the same base class library as Blazor and even uses modern SDK-style project systems for optimal tooling.”

    3.1 .NET MAUI Single Project

    The project file responsible for the collection of platform specifics is called ‘Single project’ by Microsoft. It is a shared project file that works across platforms. Single Project is built into the SDK-style of .NET 6 project sprints. The project can be separated into several sections-

    • Resources (images, icons, fonts)
    • App Manifest
    • Platform-specific code
    • NuGet Packages

    Here is what it looks like in Visual Studio-

    MAUI

    Build world-class applications
    powered by .NET 6

    4. SDK Workloads

    Dot NET 6 SDK Workloads

    SDK workloads were introduced in .NET 6 preview 4. With SDK Workloads, users now only need necessary SDKs and not a resource-intensive “all-in-one” SDK, enabling users to download and install only what they need, e.g., ASP.NET core or just Xamarin, without any additional files.

    When stripped down to its basics, SDK Workloads is a package manager for SDKs, and all of it is supported in “Workload”, a new CLI keyword.

    5 .NET 6 Preview Features

    .Net 6 now contains two new features in preview form. The idea behind this is to gather as much customer feedback as possible and then improve upon them. (These preview features are still only opt-ins, given that the final product is subject to change.)

    Here is a list of all the features and updates we have seen up until .NET 6 preview 7

    • Enhanced performance of CLI/SDK/MSBuild
    • Modernized launch/debug/update loop
    • Hot Reload- application changes during development
    • Hot Reload for Blazor
    • Hot Reload for ASP.NET Core
    • Reimplementation of the Razor compiler with source generators
    • ASP.NET Core Hot Restart
    • Improved experience for Xamarin developers
    • Reduced application startup time

    Conclusion

    So, there you have it, a rundown of all the new features that .NET 6 is about to bring in, each sure to transform the way developers approach coding. But for impeccable .NET development, you need an equally strong .NET development team.

    With over two decades of experience and the top 1% of the global development talent pool, Radixweb is your partner of choice for the Top-notch .Net development services. If you have an app development idea and wish to watch it rendered in the real world with utmost precision, then you have reached the right place. Interested? Drop us a line, and we would be happy to help.

Don't Forget to share this post!

Maitray Gadhavi is a senior business development manager with Radixweb who accelerates the growth of organizations through innovation-led custom software development. Balancing the long-term gains in an evolving industry, he brings the voice of the customers into the enterprise. Binge-watching favourite series is his idea of fun after work.