.Net CoreC#Development

New C# 8 features: Quick overview

As of this writing, C# 8 is still in Preview 5, so It’s not 100% clear if all listed features will end up in final release. C# 8 will be probably released at .NET Conf , September 23 — 25, 2019.

Nevertheless, let’s take a look at new features of C# 8.

List of reported new features of C# 8 – Preview 5 (you can find official list of new features here):

Let’s quickly check some of these new features with my comments on each of the new feature:

Default interface members

In C#8, interfaces can have default implementation. As a result, developer is not obligatory to implement this members in implementation classes. Let’s take a look:

And extract of consuming code:

Output:

My comment: To be honest, I don’t like this default interface implementation feature. Why? Because in my book, interfaces are lightweight, blueprints, contracts, abstractions and should not contain any implementation. Furthermore, I think abstract classes should be used for that purposes.

But, I agree, this default interface implementations can get handy in some cases. Default interface implementations enable developers to upgrade an interface while still enabling any implementors to override that implementation. Users of the library can accept the default implementation as a non-breaking change. If their business rules are different, they can override.

Use it with care, I will try use this new feature as less as possible – well, at least on start.

For more information about this feature, check out this link: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-members-versions

More patterns in more places

Switch expressions

Very often, switch statement produce/return value for each case. In this case, C#8 handles this situation with new switch expression mechanism. Let’s take a look.

Calling code:

Output:

My comment: In my opinion, this is nice new feature introduced in C#8. Code is cleaner and lighter (in relation to classical switch statement with return value cases).

Property patterns

The property pattern enables you to match on properties of the object examined. Let’s take a look at some exmaples:

And if I consume code, like:

I get this:

My comment: What to say: awesome new feature!

using declaration

Using declaration simplifies using statements, by letting compiler when to release important resource. Let’s look at example:

The output is:

My comment: In general good idea, but for now I will stay with explicit using statement. In my opinion when the code is more complex, this using statement can behaves as auto-magic by releasing resources auto-magically. But, as I said, generally good feature, because compiler knows at compile time when resource can be released/marked for disposal.

Static local functions

Output:

My comment: Nice new feature, but should be used carefully. In my opinion, this feature is useful because developer has ability to structure code better inside functions without usage of (type-wide) private functions. On the other hand, overuse can lead to large functions and hard to read code. So, developers should use this feature with care!

Asynchronous streams

Calling for-each loop must contain async , e.g.

My comment: Similar as with ordinary yield construct, now async iterator return result to the calling code. Nice and very useful.

Indices and ranges

Two new types and two new operators related to collection handling are introduced:

  • types: System.Index, System.Range
  • operators: ^ and ..

Let’s take a look at this method:

Output is:

My comment: Very clean and easy way of handling indexes and ranges in arrays. Very good extension to C# language!

The only awkward think here is that first item in array with index 0, but from end, operator ^1 returns last item in this collection. Something to keep in mind!

Null-coalescing assignment

New operator named null-coalescing assignment (??=) introduced in C# 8.

Definition is simple: You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.

Let’s make an example:

And the output of this method is:

My comment: I like this new feature, it can be very useful and will simplify code a lot in some cases!

Enhancement of interpolated verbatim strings

Small enhancement with string interpolation and verbatims. With pre-C#8 (Preview 5) we had this:

In C#8 (Preview 5), compiler is fine with this code:

My comment: Small, but welcome enhancement to the language.

Conclusion

Until then, happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.