[C# 8] New features

Readonly Members

Added new readonly modifiers to be used in structs, lets you define more granular properties that do not modify state.

eg consider the following mutable struct:-

public struct Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Distance => Math.Sqrt(X * X + Y * Y);

    public override string ToString() =>
        $"({X}, {Y}) is {Distance} from the origin";
}

We can decorate the ToString() method as readonly as it doesnt modify state.

public readonly override string ToString() =>
    $"({X}, {Y}) is {Distance} from the origin";

when built this would produce a compile time error like:-

warning CS8656: Call to non-readonly member 'Point.Distance.get' from a 'readonly' member results in an implicit copy of 'this'

and would require the following change to the property so that it doesnt change the state, it doesnt assume that get accessors do not modify state.

public readonly double Distance => Math.Sqrt(X * X + Y * Y);

 

Default Interface Methods

You are now able to add functionality to interfaces without breaking functionality to older versions of implemented interfaces.

interface IWriteLine  
{  
 public void WriteLine()  
 {  
 Console.WriteLine("Wow C# 8!");  
 }  
}  

This has to be used carefully as it will easily violate the Single Responsibility principles

 

Nullable reference type

Allow you to induce compiler warnings then using reference types for more defensive coding.

string? nullableString = null;  
Console.WriteLine(nullableString.Length); // WARNING: may be null! Take care!  

 

Async streams

Allow the use of Async to be now used over collections and can be iterated over.

await foreach (var x in enumerable)  
{  
  Console.WriteLine(x);  
}

Now its possible to the client consumer to be able to consume async streams in chunks as they are returned and are available.

Ranges

You can now access data sequences in 2 new ways and get various slices of data that you require.

Indices

Access to collections by taking from the beginning or the end.

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" 

Sub collection

Obtain a sub collection from the data

var slice = a[i1..i2]; // { 3, 4, 5 } 

Null-coalescing assignment

New feature that allows you to null-coalesce assign with the ??= operator so if the value on the right is assigned to the left only if the left hand is null.

List<int> numbers = null;
int? i = null;

numbers ??= new List<int>();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);

Console.WriteLine(string.Join(" ", numbers));  // output: 17 17
Console.WriteLine(i);  // output: 17

Default in deconstruction

Allows the following syntax (int i, string s) = default; and (i, s) = default

(int x, string y) = (default, default); // C# 7  
(int x, string y) = default;               // C# 8  

 

Using declarations

Enhance the using operator to be more inline such as 

// C# Old Style  
using (var repository = new Repository())    
{    
} // repository is disposed here!    
     
// vs.C# 8    
     
using var repository = new Repository();    
Console.WriteLine(repository.First());    
// repository is disposed here!