C # Three exciting new features of 8.0,

Source: Internet
Author: User

C # Three exciting new features of 8.0,

C # is released in 2000. So far, seven versions have been officially released. Each version contains many exciting new features and feature updates. At the same time, C # versions are released in the same period as Visual Studio and. the. NET runtime version is highly coupled, which helps developers better understand C # and integrate it with Visual Studio and. NET.

 

Speed up C # Release

Under the impetus of "new Microsoft", Microsoft's pace of innovation has also accelerated. To speed up the process, Microsoft's Development Department has separated some previously integrated technologies.

Visual Studio ,.. NET Framework ,. NET runtime, compiler, and runtime languages are split into their own packages and versions, which means that each of the above can be released at their own pace. Now, we have seen the success of this mode .. NET Core 2.0 has been completed, released from Visual Studio, and added support for Linux. C #7.1 is released almost the same as. NET Core 2.

C #7.1 is an important release version. Although there are not many new features, it is the beginning of C # 7th. C #7.2 has been launched, and C #7.3 is also being planned. Although the C #8.0 version is still a little far away, Microsoft technicians have some ideas about the features of C #8.0.

The discussion of language design is open, and the new features of C #8.0 are also discussed extensively. Check the milestones of each release and evaluate the future C # roadmap.

The following introduces three exciting new features in C #8.0.

 

Non-null or empty reference types

C # has two major variable types: basic type and reference type. The original types are int, char, and double. These types cannot accept null values. If a new int value is created without assigning a new value, the int value is 0 instead of null. C #2.0 introduces "?" The empty version of the variable primitive of the symbol. So, int? Is a version of int, which can accept null values.

On the other hand, the reference type (such as a string object) can always accept null values and use null as the default value. This also brings with it a disadvantage that it may lead to null references in the application.

In C #8.0, it is an optional feature to set the reference type to non-null.

It is very difficult to introduce this feature for C #, because it introduces potential compilation errors for code that has already run well. Therefore, it is necessary to create this function, rather than bringing immeasurable workload to developers.

According to the design scheme, the C # team decided to adopt a method that allows developers to select a null reference type. This will be a project-level setting to enable verification of null references. Once enabled, do I need to use an object that can accept null values? Operator.

The following code:

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

This generates a warning because the string cannot accept null values. The following code is required:

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

However, the above Code will also throw a warning on the Console, writing that you do not want to receive an empty string. In fact, the original code may have errors, so cascade warnings can help us avoid runtime errors. This is the most likely language change 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. It is a set of fields that can help you quickly create POCO-type objects. It can also solve the key issue of comparing objects with equal values.

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

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

This is a good way to create a simple class.

Use records to solve the problem of object equality

In C # programming, the most difficult thing to grasp is to use the = Operator to reference the differences between types and primitives.

For example, we use = to compare two integers:

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

Primitive values are equal. However, the reference types are not equal.

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

This is because the reference type comparison of C # considers that the reference is equal, that is, only when two objects are the same object is equal. The records type provides equivalent operators in the structure. The syntax for creating a new record is very simple, because the generated object is a simple data transmission object.

Records is a lightweight object that is very convenient to use. Although Records is not a breakthrough change in language, it is also a gradual improvement and is welcome.

 

Default Interface implementation

The version control interface may be annoying because it requires a new method on the interface to implement all objects on the interface. As new methods are added to interfaces, the tasks that implement them fall into the classes that implement interfaces. Because each implementation does not have to have the same parent class, the methods added to the interface can be implemented in their own classes.

The default Interface implementation allows you to specify an implementation in the interface, as long as it is implemented by the function of the existing method on the interface. The following uses a bank account as an example:

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

For ease of use, we want to provide clear debit and credit functions on the bank account. We usually 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}”);     } }

The default Interface implementation provides a powerful new method to extend the class that implements the interface without repeating the code. The implementation of many interfaces can be greatly simplified by following the default implementation.

 

Other new features of C #8.0

Because of these new features, we have no doubt that this is C #8.0. The following are some other new features of C #8.0:

  • Upgrade extension support-This improvement can be used not only for extension methods, but also for attributes, static methods, and more.
  • Asynchronous Data Stream-supports enumerated values that support asynchronous operations. Includes the new iasyncenumerable <T> and iasyncenumerator <T> interfaces.
  • Async Disposable-iasyncdisposable allows an asynchronous Processing Method for an object.

 

Conclusion

In the past few years, the speed of. NET innovation has indeed accelerated. Although C #8.0 has not yet been implemented, it will bring a lot of help improvements compared with C #7.0. Let's look forward to the early arrival of C #8.0.

Link: https://dzone.com/articles/3-new-c-8-features-we-are-excited-about

For more information, see grape city control.

 

Related reading:

[Report benefits] More than 100 Report Templates can be downloaded for free.

Why do you need to migrate code to ASP. NET Core 2.0?

What optimization causes. NET Core performance to soar?

C #13 things developers should know

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.