C# 8 is moving aggressively into the innovation world. Because of this strategy, Microsoft left many developers in a confused state. Currently, the developers have a very controversial opinion on the C# 8 new features. In this article, you can have a brief overview of the new proposed features of C# 8 language. I will describe the important features separately and demonstrate them with an example; the other less important features I will handle briefly. I will also write about the pros and cons of each important upcoming feature. Furthermore, I will give a rating score for each feature between 1 (Bad) and 10 (Excellent), and finally, I hope after reading this article, you will have a clear vision of the C# 8 Features.
The rating score is based on the below-listed criteria,
- The technology behind: It is based on a proven and reliable technology or not?
- Innovation: Did Microsoft do any innovation in the proposed feature?
- My personal opinion: Based on my discussions with the communities and my expertise.
Important C# 8 stuff
C# 8 Features
The new C# 8 features,
- Default interface methods
- Nullable reference types
- Pattern matching enhancements
- Asynchronous streams / Asynchronous disposable
- Using declarations
- Enhancement of interpolated verbatim strings
- Null-coalescing assignment
- Static local functions
- Indices and ranges
- Unmanaged constructed types
- Readonly-Member
- Stackalloc in nested expressions
- Disposable ref structs
Default Interface Methods
It allows you to add new functionality to the interfaces of your libraries and ensure the backward compatibility with code written for older versions of those interfaces.
Example
- interface IWriteLine
- {
- public void WriteLine()
- {
- Console.WriteLine("Wow C# 8!");
- }
- }
GitHub
Status: Prototype
Pros
You can add new functionality to the interface without breaking the compatibility with the older versions of those interfaces.
Cons
It should be used carefully. Otherwise, it can easily lead to violating the single responsibility principles.
Community
It is a very arguable feature, and it left many open discussions among the .NET community. I have handled this theme in depth in Reddit:
Innovation
There is nothing new here. This concept is already applied in many languages and cloned from Java.
Technique behind
It is based on traits technique, and traits is a proven, powerful technique in OOP.
For more information about traits proof theory:
My personal opinion
I give this feature score 3. My tip you should think before using it and ask yourself, is there any other elegant solution, which might fit better to solve your problem?
For more detailed information about this feature, you can read my article.
Nullable reference type
Emits a compiler warning or error if a variable that must not be null is assigned to null.
Example
- string? nullableString = null;
- Console.WriteLine(nullableString.Length); // WARNING: may be null! Take care!
GitHub
https://github.com/dotnet/roslyn/tree/features/NullableReferenceTypes
Status: Preview
Pros
It helps you to eliminate the NullReferenceException (The Billion Dollar Mistake, ALGOL60, Tony). Moreover, it helps you to solve the problems in code like a pyramid of doom.
Cons
However, in complex scenarios this feature may bring some confusion in regards to reference types and using “?” character for more information, please read https://codeblog.jonskeet.uk/2019/02/10/nullableattribute-and-c-8/.
Community
In general, this feature is well accepted among the .NET Community.
Innovation
Good innovation from Microsoft.
Technique behind
A proven technology.
My personal opinion
I give this feature 9. Yes, please use it! And, get rid of the NullReferenceException!
More Info
Advanced Pattern Matching
It provides the ability to deconstruct matched objects, giving you access to parts of their data structures. C# offers a rich set of patterns that can be used for matching.
Example
- var point = new 3DPoint(1, 2, 3); //x=1, y=2, z=3
- if (point is 3DPoint(1, var myY, _))
- {
- // Code here will be executed only if the point .X == 1, myY is a new variable
- // that can be used in this scope.
- }
GitHub
Status: Preview
Pros
Recursive pattern matching helps you decompose and navigate data structures in a very convenient, compact syntax. While pattern matching is conceptually similar to a sequence of (if, then) statements, so it will help you to write the code in a functional style.
Cons
In complex expressions, the syntax might be tricky and difficult to understand.
Community
Well accepted among the .NET Community.
Innovation
There is nothing new here.
Technique behind
Pattern Matching is a proven and known technology, which has been used for many years especially in functional programming.
My personal opinion
I give this feature 9. I expected pattern matching in C# will be trendy like LINQ and Lambda expressions. This feature can be very good combined with Records to make C# syntax more functional, but I am not sure that the Records will be released in the next C# 8 version or not.
You can find more information about PM in my Pattern Matching Introduction:
Async streams
Allows using ‘async’ to iterate over collections.
Example
- await foreach (var x in enumerable)
- {
- Console.WriteLine(x);
- }
Pros
Async Streams provide an excellent way to represent asynchronous data sources that can be controlled by the consumer. For example, when downloading data from the web, we would like to create an asynchronous collection that returns the data in chunks as they become available.
Community
Well accepted.
Innovation
There is nothing new here! This technique is already used in many other languages Akka Streams.
Technique behind
Async Pull Programming Model is known and used for years.
My personal opinion
I give this feature 7. Please use it with the correct use cases like, downloading data from the web in chunks or getting a big query result in chunks from the database.
More Info
Ranges
Ranges are a very powerful construct when it comes to accessing sequences of data or getting a slice from a collection. This feature consisting of two parts,
Part I Index
Which can be used to obtain the collection from the beginning or from the end.
Example
- Index i1 = 3; // number 3 from beginning
- Index i2 = ^4; // number 4 from end
- int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6"
Part II Range
Access a sub-collection(slice) from a collection.
Example
- var slice = a[i1..i2]; // { 3, 4, 5 }
Pros
Nice syntax to access sequence of data from a collection.
Innovation
And again, nothing is new here. A lot of similar technology is already used in other languages like Python.
Community
Widely accepted.
My personal opinion
I give this feature an 8. It can make the code more readable and understandable.
More Info
Caller Expression Attribute (Not C#8, Changed to C#Next)
Allows the caller to 'stringify' the expressions passed in at a call site. The constructor of the attribute will take a string argument specifying the name of the argument to stringify.
Example


Rayko Azcue PerezPosted May 6, 2019, 11:38 AM
Thanks for the overview and shared opinion..
Bassam AlugiliPosted Apr 11, 2019, 2:19 PM
thank you too.
Vinay KPosted Apr 2, 2019, 1:56 AM
Thank you.....
Bassam AlugiliPosted Mar 26, 2019, 8:09 AM
I would like also to thank Thomas Albrecht (https://www.xing.com/profile/Thomas_Albrecht35) for reviwing this article!
Rajanikant HawaldarPosted Mar 25, 2019, 1:07 AM
Thanks for sharing
Ajay GuptaPosted Mar 24, 2019, 6:05 AM
Thank you for update us . Good one
Mahesh ChandPosted Mar 23, 2019, 9:50 PM
Thank you, Bassam for details on C# 8 features. Welcome to C# Corner.
Faisal PathanPosted Mar 23, 2019, 7:29 PM
Outstanding article!