Five Things You Should Know About .NET 5
.Net 5

Five Things You Should Know About .NET 5

NET 5, the successor of .NET Core 3.1 and .NET Framework 4.8, aims to provide .NET developers with a new cross-platform development experience. It puts order to the .NET universe fragmentation that has been arising over the years and brings new amazing features. Of course, you cannot learn all about .NET 5 right now, but you can focus on just five things to have a clear understanding of what is going on.

The Unified Platform

The first thing you have to know is that .NET 5 brings you a new unified vision of the .NET world.

The standardization allowed anyone to create their own implementation of the .NET runtime. And in fact, a few of them appeared on the horizon:?Mono?for Linux-based systems,?Silverlight?for browser-based applications, .NET Compact?and?Micro?frameworks for mobile and resource-constrained devices, and so on. Then, Microsoft decided to write .NET Core from scratch with cross-platform compatibility in mind.

New Features in C#

The second thing you should know is about C#. .NET 5 comes with C# 9, the new version of the .NET platform's main programming language. There are several new features, but here you will find a taste of the most relevant ones.

Top-level statements

Among the new features, one of the most notable is the introduction of?top-level statements. To learn what they are, take a look at the following classic minimal program:

using System;

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

Just to write one string to the console, you need to define a namespace, a class, and the static method?Main(). Now, you can get rid of this code infrastructure and simply write the following:

System.Console.WriteLine("Hello World!");        

Top-level statements allow you to focus on what really matters in small console programs and utilities and use C# with a more scripting-oriented approach.

Record types

Another interesting new feature is?record types. With records, you can declare an immutable reference type, i.e., a class-based type that cannot be changed after its creation. An example of a built-in immutable reference type is the?System.String?class. After you create an instance of a?System.String, you cannot change its value anymore.

Consider the following record type declaration:

public record Person
{
    public string FirstName { get; }
    public string LastName { get; }

    public Person(string first, string last) => (FirstName, LastName) = (first, last);
}        

You can create an instance of the?Person?record as you do with a class, but you can't change its?FirstName?property:

var person = new Person("John", "Doe");

person.FirstName = "Jack";    //throws an error        

However, you can compare two instances of the?Person?record similarly to primitive values:

var person = new Person("John", "Doe");
var anotherPerson = new Person("John", "Smith");

Console.WriteLine(person == anotherPerson);  //false        

Init setters

C# 9 also adds the?init?accessor to define?properties that can only be initialized. To better explain its use cases, consider the following class definition:

public class Person {
    public string FirstName { get; init; }
    public string LastName { get; init; }
    public string Address { get; set; }
}        

This class defines a person with?LastName?and?FirstName?properties that can be initialized, but not changed. The?Address?property can be changed at any time:

var person = new Person {
    FirstName = "John",
    LastName = "Doe",
    Address = "124 Conch Street, Bikini Bottom, Pacific Ocean"
}

person.Address = "17 Cherry Tree Lane";
person.FirstName = "Jack";    //throws error        

If you want to learn more about the features brought by C# 9, please?check out the official documentation.

.NET MAUI, the Universal UI

As the third thing, you have to know that .NET 5 is bringing to you a new way to build cross-platform user interfaces. Thanks to the?.NET Multi-platform App UI?framework, also known as?.NET MAUI), you will be able to build user interfaces for Android, iOS, macOS, and Windows with a single project.

Actually, this feature is still in progress and will be released with .NET 6, but?you can start taking a look at .NET MAUI?to be ready when it is officially released. Take a look at?the roadmap?to follow its progress.

.NET MAUI can be considered an evolution of?Xamarin.Forms, the open-source framework for building iOS and Android apps with a single .NET codebase. But this new framework proposes a universal model for building UIs on mobile and desktop platforms.

In addition to the well-known?Model-View-ViewModel?(MVVM) pattern, .NET MAUI supports the new?Model-View-Update?(MVU) pattern. This is a one-way data flow pattern inspired by the?Elm programming language architecture?that provides an effective way to manage UI updates and application state.

Supporting Single-File Applications

The fourth thing you will get in .NET 5 is the support of?single-file applications, that is, applications published and deployed as a single file. That means that your application and all its dependencies are bundled into one file. For example, say you run the following command within the folder of your .NET 5 project:

dotnet publish -r linux-x64 --self-contained true /p:PublishSingleFile=true        

You will get a single file containing your application built for Linux, all the dependencies you used in your project, and the .NET runtime (--self-contained?true). This means that you even don't need to install the .NET runtime on the target machine.

Of course, you can also specify these parameters in your project configuration:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <!-- Enable single file -->
    <PublishSingleFile>true</PublishSingleFile>
    <!-- Determine self-contained or framework-dependent -->
    <SelfContained>true</SelfContained>
    <!-- The OS and CPU type you are targeting -->
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
  </PropertyGroup>

</Project>        

Be aware that this feature doesn't use the same approach as the single-file applications you can build in .NET Core 3.1. In .NET Core 3.1, the single-file application is just a way to package binaries. At run time, they are unpackaged to a temporary folder, loaded, and executed. In .NET 5, the single-file application has a new internal structure, and it is directly executed with no performance penalty.

No Longer Supported Technologies

The last of the five things you are learning about .NET 5 concerns what is no longer supported. As said above, the architectural review and the attempt to make .NET 5 an actual cross-platform programming framework led to removing a few features supported in .NET Framework. Let's take a quick look at the removed features and the possible alternatives.

Web Forms

For a long time,?ASP.NET Web Forms?has been the main technology to build dynamic web UIs. However, it is not a secret that its lifetime was closely bound to .NET Framework's destiny. .NET Core doesn't support Web Forms, so the fact it is no longer supported in .NET 5 shouldn't actually be news.

However, you have a few alternatives to build web UIs. If you are building traditional web applications,?Razor Pages?are one of these alternatives. If you want to build single-page applications, you can use?Blazor.

Windows Communication Foundation (WCF)

Even?WCF, the traditional communication framework for Windows, is going to be deprecated. This may appear a bit shocking for the developers that have used it to build their service-oriented applications. However, it is pretty understandable if you realize that the primary goal of .NET 5 is becoming a cross-platform framework.

The alternative to WCF recommended by Microsoft is to migrate to?gRPC. But if you are nostalgic about WCF or want to prepare a smooth transition, you can give the?CoreWCF?open-source project a try.

Windows Workflow Foundation

Finally, .NET 5 will not even include?Windows Workflow Foundation, the workflow engine technology available in .NET Framework. There is no official replacement for this technology. However, you can use an open-source porting project,?CoreWF, to attempt moving your existing workflows on .NET 5 or creating new ones.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了