In this post, we will be talking about the first four new features of C# 8. This is a major release after C# 7.
Topics Covered
Introduction
C# (pronounced as CSharp) is an object-oriented, type-safe, and managed language that is compiled by .NET Framework to generate Microsoft Intermediate Language (MSIL).
New Features Of C# 8
Below are the four features that Mads Torgersen discussed in his recent talk on channel 9.
1. Nullable Reference Types
With C# 8.0, the reference type would not be nullable by default. Assigning null to a non-nullable reference type will be a compiler warning, similarly reading from a nullable type would also be a compiler warning unless the variable in question was explicitly checked for null ahead of time.
2. Async Streams
Async streams are the asynchronous equivalent of IEnumerable. The syntax for the same is:
foreach
await
(string
s in
asyncStream)
3. Default Interface Implementations
A default interfaces programming capability, so interfaces can evolve via virtual extension methods. An API author could add methods to an interface in future versions without breaking source or binary compatibility. This feature already is available in programming languages such as Java.
The most cited use case for this feature is the ability to add a Count property to IEnumerable<T>. The idea is that instead of using the Enumerable.Count
extension method, developers can get Count for free and optionally override it if they can provide a more efficient alternative.
interface IEnumerable { int Count() { int count = 0; foreach (var x in this) count++; return count; } } interface IList { int Count { get; } override int IEnumerable.Count() => this.Count; }
4. Extension Everything
Extension properties were the long-time pending feature. Under the new design, there is a new top-level construct called an extension. For example, if you want to create extension methods and properties for a Student class you would write:
extension StudentExt extends Student { // methods and properties go here }
👉 Just try your hands with these new features of C# 8 and have fun playing around.
We hope you like the post and the explanation of the new features of C# 8. Please share a word with your fellow C# developers.
Interested to know how C# Evolved?
Check 👉 C# Version History
What do you think?
Dear Readers,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share it along with your friends and help them to learn.
Happy Coding!