C# 8.0 most awaited Features
One of the most loved and popular programming language of the present time. With the new arrival of C# coming nearer and nearer we all are pretty much excited about the major release of C# Version 8. C# has been around for three decades and it is expected to release it’s latest version in 2019 and everyone is curious about the new features that it will carry.
Looking back at the history, every new release of C# has coupled up with new version of Visual Studio and .Net runtime. With the release of C# 7.2 and 7.3 now it’s time for C# 8,0which will soon make it to our doorsteps. We can say that it is coming with cool features which will come alive with Visual Studio 2019. But, We already have a slight information about what we can wait for in the new version. Some of the other improvements will tickle out in the coming days.
The motto of this article is to provide you with an overview of what to expect from C# 8.0 :
Nullable Reference Types:
This feature will help to prevent the null reference exceptions that have bothered developers for many years.
It prevents you from putting null into ordinary reference types .
But there can be a situation where you need a nullable reference type, such as string. In such case the new thing would be nullable types.
The same syntax i.e. question mark will be used for nullable reference type.
For eg: string? s = null; // Ok
Async Streams:
The previous version of C# was not helpful if you wanted to produce or consume continuous streams of data. Like you could get from IoT device or cloud service.
Async Streams will provide an abstraction that is an asynchronous version of IEnumerable. i.e IAsyncEnumerable<T>
For Example:
async IAsyncEnumerable<int> GetBigResultsAsync()
{
await foreach (var item in GetResultsAsync())
{
if (result > 10) yield return result;
}
}
Default Implementations of Interface Members:
Now it will be easier to work with Interface. In the present you can’t add members to Interface without breaking source or binary compatibility. But now if someone doesn’t implement the member, the developer will get default implementation instead. This type of feature is already available in programming language like java.
For Example:
interface ILogger
{
void Log(LogLevel level, string message);
void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); // New overload
}
class ConsoleLogger : ILogger
{
public void Log(LogLevel level, string message) { ... }
// Log(Exception) gets default implementation
}
Light Weight Classes(Records):
Another great feature of C# 8 is a way to create a C# class called records.These are light weight classes that is collection of fields.
It heps in the fast creation of object and also for the comparison of objects.
For Example:
Creating a simple Student class would look like:
Class Student { int ID, string name, string department }
It will be expanded into a larger class implementing IEquatable.
Recursive Patterns:
Recursive patterns are the type of patterns that can contain other patterns.The new version of C# will allow recursive patterns.
IEnumerable<string> GetEnrollees()
{
foreach (var p in People)
{
if (p is Student { Graduated: false, Name: string name }) yield return name;
}
}
Switch Expressions with light version of switch statements:
Switch statements can be cumbersome to write. So the C# 8.0 will come up with light version of switch case where all cases are expressions.
For Example:
var area = figure switch
{
Line _ => 0,
Rectangle r => r.Width * r.Height,
Circle c => Math.PI * c.Radius * c.Radius,
_ => throw new UnknownFigureException(figure)
};
Target Typed new Expressions:
When we are creating a new object, the type is already provided from the context. In such cases it will let us omit the type:
This feature was introduced by one of the member of .NET community
For example:
Point[] ps = { new (1, 4), new (3,-2), new (9, 5) }; // all Points...................
Almost all the features of C# 8.0 will work on any version of .NET. But few of them can have platform dependencies like the Async streams, indexers and ranges all will depend on the new framework type i.e. DotNet Standart 2.1 .
Also Read: Best Laptops for Programmers