Three exciting new features of C# 8.0

The C# language was released in 2000 and has been officially released in 7 versions, each of which contains many exciting new features and functional updates. At the same time, each version of C# is highly coupled with the concurrent Visual Studio and .NET runtime versions, which also helps developers better learn to master C# and combine it with the use of Visual Studio and .NET .

 

Speed ​​up the release of the C# version

The pace of innovation at Microsoft has also accelerated, driven by what has been dubbed the "new Microsoft." To speed things up, Microsoft's development division has separated out some of the technologies that used to be integrated together.

Visual Studio, .NET Framework, .NET runtime, compilers, and runtime languages ​​for compilation runtimes are all split into their own packages and versions, which means that each of the above can be released at its own pace. Now, we have seen the success of this model. The work on .NET Core 2.0 has been completed and released from Visual Studio, which also adds support for Linux. Around the same time .NET Core 2 was released, C# 7.1 was released.

C# 7.1 is a major release, and while there aren't many new features, it's the start of the 7th major version of C#. Work has already started on the C# 7.2 release, and a C# 7.3 release is also planned. Although the C# 8.0 version is a bit far off, Microsoft techs already have some thoughts on the features of the C# 8.0 version.

The language design discussion is open, and the issues section surrounding the new features in C# 8.0 is discussed extensively. Check out the milestones for each release, and also do an assessment for the future C# roadmap.

Here's the official start of three exciting new features in C# 8.0.

 

non-null and nullable reference types

C# has two types of variables: primitive types and reference types. The original types were int, char, and double. None of these types can accept null values. Creating a new int value without assigning a new value results in an int value of 0 instead of null. C# 2.0 introduced a nullable version of the variable primitive with the "?" symbol. So, int? is a version of int that can accept null values.

On the other hand, reference types (objects like strings) are always able to accept null values ​​and have null as their default value. This also brings a disadvantage, that is, it may lead to the introduction of null references in the application.

In C# 8.0, making reference types non-null became an optional feature.

Introducing such a feature to C# is difficult because it introduces potential compilation errors into code that is already working fine. So what needs to be done is a way to create this functionality that doesn't impose an immeasurable amount of work on the developer.

Based on the design, the C# team decided to take an approach that would allow developers to choose between nullable reference types. This will be a project-level setting to enable validation of nullable references. Once enabled, objects that accept null values ​​need to be used? operator declaration.

The following code:

String s = null;
Console.Write(s);

This will cause a warning because strings cannot accept null values. So the following code is required:

String? s = null;
Console.Write(s);

However, the above code will also throw a warning in the Console, writing that it is not expected to receive an empty string. In fact, the original code may have errors, so cascading warnings can help us avoid runtime errors. This is the language change most likely to improve code quality.

 

New lightweight class: Records

A great new feature of C# 8.0 is a new way to create a class called records. This class is essentially a very lightweight class, a collection of fields that can help quickly create POCO-type objects, and also solve the key problem of comparing objects for equality.

For example, create a record type for a bank account:

class BankAccount(Guid Id, string Name, decimal Balance)

This is a great way to create simple classes.

Use records to solve object equality problems

One of the hardest things to grasp in C# programming is the difference between using the == operator for reference types and primitives.

To illustrate, we use == to compare two integers:

int I = 1;
int j = 1;
i == j //yields true

Primitive values ​​are equal. However, for reference types, it is not equal.

Object I = new Object();
Object j = new Object();
i == j //yields false

This is because C#'s reference type comparisons consider reference equality, that is, two objects are equal only if they are the same object. The records type provides structural equality, equivalent to the equality operator. The syntax for creating a new record is very concise because the resulting object is a simple data transfer object.

Records is a lightweight object that is very convenient to use. While Records is not a breaking change to the language, it is an incremental improvement that is welcome.

 

Default interface implementation

Versioning an interface can be frustrating because it requires new methods on the interface to implement all objects on the interface. As new methods are added to the interface, the task of implementing them falls to the individual classes that implement the interface. Because implementations do not have to share the same parent class, methods added to an interface can be implemented in their own classes.

Default interface implementation allows specifying an implementation in an interface as long as it is a function implementation of an existing method on the interface. Here's another example of a bank account:

public interface IBankAccountManager{
    void PerformTransaction(decimal amount, string reason);
}

Now for ease of use, we want to provide explicit debit and credit functionality on bank accounts. Usually we add these functions to interfaces and implement them in all classes.

public interface IBankAccountManager{
    void PerformTransaction(decimal amount, string reason);
    void PerformDebit(decimal amount, string reason){
        PerformTransaction(-1 * amount, $”Debit: {reason}”);
    }
 
    void PerformCredit(decimal amount, string reason){
        PerformTransaction(amount, $”Credit: {reason}”);
    }
}

默认接口实现提供了一种强大的新方法来扩展实现接口的类,而无需重复代码。只需遵从默认实现,许多接口的类的实现就可以得到大大简化。

 

其它的 C# 8.0 新特性

正是因为这些新特性,才让我们毫不怀疑这就是 C# 8.0。以下是其它一些 C# 8.0 新特性:

  • 提升扩展支持 – 这种提升不仅仅可以用于扩展方法,它还提供了对属性、静态方法和更多方面的支持。
  • 异步数据流 – 能够拥有支持异步操作的枚举值。包括新的 iasyncenumerable <T> 和 iasyncenumerator <T> 接口。
  • Async Disposable – iasyncdisposable 允许对象有一个异步的处理方法

C#开发工具推荐

ComponentOne Studio Enterprise 是一款专注于企业应用的 .NET 全功能控件套包,支持 WinForms、WPF、UWP、ASP.NET MVC 等多个平台,帮助您在缩减成本的同时,提前交付丰富的桌面、Web 和移动企业应用。控件支持的范围广泛,包含了表格和数据管理、图表和数据可视化、流行的UI界面等,为企业应用开发提供高性能的控件工具。

结论

过去几年中,.NET 的创新速度确实加快了。虽然 C# 8.0 目前还没有实现,但是它比起 C# 7.0,将带来很多有帮助的提升,让我们一起期待 C# 8.0 的早日到来。

原文链接:https://dzone.com/articles/3-new-c-8-features-we-are-excited-about

转载请注明出自:葡萄城控件

 

关于葡萄城

葡萄城成立于1980年,是全球最大的控件提供商,世界领先的企业应用定制工具、企业报表和商业智能解决方案提供商,为超过75%的全球财富500强企业提供服务。葡萄城于1988年在中国设立研发中心,在全球化产品的研发过程中,不断适应中国市场的本地需求,并为软件企业和各行业的信息化提供优秀的软件工具和咨询服务。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326621492&siteId=291194637