Top 5 new features of C# 8 (My take)

C# 8 was recently released with .Net Core 3.0. These two were released simultaneously in .Net Conf 2019.

There are a lot of new features are released as a part of C# 8. Some of them are very interesting. Whereas some not so.

C# 8 top 5 features

C# 8 Top 5 Features

Below are the 5 features, which stands out for me. I am ordering from number 5 to number 1.

5. Readonly members

Now we can apply readonly modifier to any member of a struct. This makes a lot of sense for struct as its a value type.

I do not use struct on a day to day basis. But there are definitely a lot of situations where struct makes a lot of sense. Espacioally if you need faster response.

Usage:

public struct Coordinates
{
    public readonly double Long { get; set; }
    public readonly double Lat { get; set; }
    public readonly Tuple<long, long> Point =>(Long, Lat);
}

4. Using declaration

The using declaration now has a shortcut. Instead of writing this way:

public void CreateEmployee(Employee employee)
{
    using (var con = new SqlConnection("<connection string>"))
    {
        // Code to write to db
    } // connection is disposed here
}

Now can be written as:

public void CreateEmployee(Employee employee)
{
    using var con = new SqlConnection("<connection string>");
    // Code to write to db
}  // connection is disposed here

3. Default interface method

We can now add default method to an interface. The idea is that we can add a method later to an interface with a default method, so that existing implementations does not fail.

I get the idea, but I am not very sure how widely useful this feature will be. And how much of confusion it might create. It is inline with Trait in other languages like Scala.

Below is one scenario where I see it makes sense. In this scenario, we have an IOrder interface, which can have a default implementation of the property MarketValue, as it will always be the multiplication of the Price and Quantity.

public interface IOrder
{
    DateTime Purchased { get; set; }
    decimal Price { get; set; }
    int Quantity { get; set; }
    decimal MarketValue => Price * Quantity; 
}

2. Asynchronous streams

This is one of my favorites. Now we can create and consume streams asynchronously.

Declaration:

public static async IAsyncEnumerable<int> GenerateRandom()
{
    Random rnd = new Random();
    for (int i = 0; i < 20; i++)
    {
        await Task.Delay(100);
        yield return rnd.Next(1000);
    }
}

Reading:

await foreach (var number in GenerateRandom())
{
    Console.WriteLine(number);
}

1. Property patterns

And last but not the least pattern matching on property. This is one is my absolute favorite. To be able to match on a property is priceless. I love the fact that we can provide the name of the property without using object notation.

Example:

public decimal Population(Address address) =>
    address switch
    {
        { Zip: "09876" } => 1.5M,
        { Zip: "56789" } => 1.6M,
        { Zip: "12345" } => .9M,
        _ => 0
    };

Conclusion

These are the 5 features which I found to be very exciting. Let me know what is your favorite feature in  C# 8. In my future articles, I will walk through details of some of these features. As features like pattern matching and asynchronous streams need a deep dive to understand the features completely.

References:

For more information on C# 8 features, please refer to the Microsoft documentation https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

YouTube video link the article in action is here: https://www.youtube.com/watch?v=wP5MHJ2UVNI