The C# language was released in 2000 and has since been officially released in seven versions, each containing many exciting new features and functional updates. At the same time, each release of C# is highly coupled with the Visual Studio and.net runtime versions of the same period, which helps developers learn about C# and integrate it with Visual Studio and.net.

 

Speed up C# releases

The pace of innovation at Microsoft has also accelerated, driven by what has been dubbed the “new Microsoft”. In an effort to speed things up, Microsoft’s development department is separating technologies that used to be integrated together.

Visual Studio, the.NET Framework, the.NET runtime, the compiler, and the runtime language for compiling the runtime have all been split into their own packages and versions, which means that each of these can be released at its own pace. Now, we are already seeing the success of this model. Work on.NET Core 2.0 has been completed and released from Visual Studio, along with added support for Linux. Around the same time.net Core 2 was released, C# 7.1 was released.

C# 7.1 is an important release, and while there are few new features, it is the beginning of the seventh major release of C#. Work on C# 7.2 is already underway, and C# 7.3 is also planned. Although C# 8.0 is still a bit far off, Microsoft technicians have some ideas about the features of C# 8.0.

The language design discussion was open, and the issues section around the new features in C# 8.0 was also extensively discussed. Review the milestones for each release and also assess the future C# roadmap.

Let’s begin with three exciting new features in C# 8.0.

 

Non-null and nullable reference types

C# has two main variable types: primitive and reference. The original types are 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 the “? Nullable version of the variable primitive for symbol. So int? Is a version of int that accepts null values.

On the other hand, reference types (objects such as strings) can always accept null values and use NULL as the default. This brings with it a disadvantage that it can lead to empty references in the application.

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

Introducing such a feature into C# is difficult because it introduces potential compilation errors into code that already works well. What needs to be done, therefore, is to create a way to do this functionality without creating an incalculable amount of work for developers.

Based on the design, the C# team decided to adopt an approach that allowed developers to choose nullable reference types. This will be an engineering level setting to enable validation of nullable references. Once enabled, objects that accept null values need to use? Operator to declare.

The following code:

String s = null;
Console.Write(s);Copy the code

This raises a warning because strings cannot accept null values. Therefore, the following code is required:

String? s = null;
Console.Write(s);Copy the code

However, the above code also throws a warning on the Console that it does not want 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 in 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 helps quickly create objects of type POCO, while also solving the critical problem of comparing objects for equality.

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

class BankAccount(Guid Id, string Name, decimal Balance)Copy the code

This is a great way to create simple classes.

Use records to solve object equality problems

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

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

int I = 1;
int j = 1;
i == j //yields trueCopy the code

The values of primitives are equal. However, for reference types, this is not the case.

Object I = new Object();
Object j = new Object();
i == j //yields falseCopy the code

This is because C#’s reference type comparison takes reference equality into account, that is, two objects are equal only if they are the same object. The Records type provides structural equality, the equivalent equality operator. The syntax for creating a new Record is very neat because the generated objects are simple data transfer objects.

Records is a lightweight object that is very easy to use. While Records is not a ground-breaking change in the language, it is a welcome incremental improvement.

 

Default interface implementation

Versioning interfaces can be frustrating because they require 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 classes that implement the interface. Because implementations do not have to share the same parent class, methods added to interfaces can be implemented in their own classes.

The default interface implementation allows you to specify an implementation in an interface, as long as it is implemented as a function of an existing method on the interface. Here’s another example of a bank account:

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

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

public interface IBankAccountManager{
    void PerformTransaction(decimal amount, string reason); 
    void PerformDebit(decimal amount, stringreason){ PerformTransaction(-1 *The Debit amount, $" : {" reason} "); }void PerformCredit(decimal amount, stringReason) {PerformTransaction (Credit amount, $" : {" reason} "); }}Copy the code

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

 

Other C# 8.0 new features

Because of these new features, there is no doubt that this is C# 8.0. Here are some other new C# 8.0 features:

  • Promotion extension support – This promotion can be used for more than just extension methods, it also provides support for properties, static methods, and more.
  • Asynchronous data flows – The ability to have enumerated values that support asynchronous operations. Includes the new IAsyncenumErable

    and iAsyncenumerator

    interfaces.

  • Async Disposable – iAsyncDisposable allows an object to have an asynchronous processing method.

 

conclusion

The pace of.net innovation has really accelerated over the past few years. While C# 8.0 is not currently implemented, it offers a number of useful improvements over C# 7.0, so let’s look forward to C# 8.0 coming soon.

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

Reproduced please indicate from: Grape city control

 

Related reading:

More than 100 sets of report templates are free to download

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

What optimizations are causing.NET Core performance to soar?

13 things C# developers should know

 



This article is published by grape city control technology development team, reprint please indicate source: Grape city control

For more development tools and tips, please go to the official website of grape City controls

To learn about enterprise reporting and Web applications, visit the Grape City Enterprise Software website