Some time ago I covered one of the new features coming in C# Version 8, Nullable Reference Types. With 2018 coming to a close, we are hopefully getting close to finally having a release date. A preview of C# Version 8 with the biggest and most anticipated new feature, Nullable Reference Types, is now available.
Now I wanted to take some time to go though some of the other new features that, while not massive in terms of development impact, are nonetheless useful and worth talking about.
One of the bigger, and probably more tedious, annoyances in development is being in the middle of coding the more important parts and suddenly having to detour to create boilerplate code. A large majority, or in some cases all, boilerplate code is data container classes. In barest form, they usual start out as something like this:
Then to make sure all Widget objects with the same power and size are equal in terms of comparing and hashing, it’s often necessary to add IEquatable interface.
For C# Version 8, the “Records” feature offers a simple yet effective solution that automatically generates the above with a single line of code:
Some decisions remain open on the default of this feature. You can find out more about this feature here.
Default Interface Methods
This feature addresses two shortcomings with interfaces. The first is the allowance of static methods inside interface declarations which can be optionally overridden by implementers using it. The second part of this feature is allowing for non-public access modifiers.
Allowing methods inside interface declarations provides for a limited form of multiple inheritance without collision. For example:
Now, define a class using this interface. Note that the class doesn’t have to implement these.
Creating the class as the interface provides the method.
But the class doesn’t inherit this method, so the following generates a compiler error.
Because of this, it’s possible to create multiple interfaces with the same named method, but without a collision. The method used would be based simply on the current casting of the instance. In addition derived interfaces can override these methods.
Extensions began in C# Version 3 to support LINQ and is currently are limited to objects. For example, a class named Person:
Can be extended to add a method named GetFullName:
The proposed feature enhancement lets you extend properties like this:
More information on this feature is available here.
The asynchronous streams feature adds two interfaces, IAsyncEnumerable<T> and IAsyncEnumerator<T>, to address a major shortcoming in the ability to do true asynchronous stream handling. To get a better understanding of how this comes into play, lets go through an example that generates Fibonacci sequence to represent the asynchronous stream. A Fibonacci sequence starts with 0,1 (or 1,1 traditionally) and each new number is the sum of the prior two number, thus going to 10 numbers of the sequence would be 0, 1, 1, 3, 5, 13, 21, 34, 55, 89.
The basic, synchronous form is the following:
Using the current available asynchronous features of C#, we can create the following:
The problem with this is while we’ve made it possible use this method asynchronously, it is still blocking the thread between each yield. It’s possible to change this to return a collection, but this prevents processing each loop interaction. If there were actual stream data coming in this wouldn’t work as the loop could go on for long time and nothing would be handled as it arrives.
The new Asynchronous Streams feature addresses this by adding the IAsyncEnumerable<T> and IAsyncEnumerator<T> interfaces. Using these, we can change the method to this:
This can be consumed using:
No Release Date Yet
There is still no firm release date on these features, but it is likely the release will coincide with the recently announced plans for Visual Studio 2019. At this time there haven’t been any dates given for when Visual Studio 2019 will be available for preview. Microsoft has directed those interested in the Visual Studio 2019 preview to visit https://visualstudio.microsoft.com/vs/preview/ and subscribe there to receive future notifications.
John Montgomery, Director of Program Management for Visual Studio, gives some insight into the functionality changes planned:
“Expect more and better refactorings, better navigation, more capabilities in the debugger, faster solution load, and faster builds. But also expect us to continue to explore how connected capabilities like Live Share can enable developers to collaborate in real time from across the world and how we can make cloud scenarios like working with online source repositories more seamless. Expect us to push the boundaries of individual and team productivity with capabilities like IntelliCode, where Visual Studio can use Azure to train and deliver AI-powered assistance into the IDE.Need Help?”
John’s full blog post on Visual Studio 2019 is available here.
KTL Solutions provides customized software solutions, Office 365 support, eCommerce customizations, and integration with Microsoft Dynamics. As a Dynamics Partner, KTL has the knowledge and experience to help with migration, deployment, customization development, and support for Dynamics 365. We understand that every organization is unique with its challenges and needs. Our experience working with hundreds of clients will provide best practices, recommend improvements, and the software customizations you need to help your business succeed.
If you’re interested in working with KTL, get in touch with us today.