C# 8 Preview

I recently watched the preview of C#8 with Mads Torgersen and Seth Juarez (here). I would recommend giving it a watch. But if you don't have time I've written a summary of the new features they talked about in this article. 

(It's probably important to say C#8 is still in the planning stages, so some of these features may change)

Nullable Reference Types

We are all familiar with nullable value types in C#. At the moment you can declare a type as either int or int? (for example). The int type cannot be null and if you want to use the int? type as an int (i.e. get the value of it) its up to you to perform the necessary checks on the value that it isn't null.

However, for reference types, there is currently no such thing as a "nullable reference type". Creating all those "null reference exceptions" in our production system we hate. For exampe:
string s = null;  
int len = s.Length;  
is valid C# but will produce a runtime error.

Well, the plan is to change that for C#8. With the addition of nullable reference types. These will use the same syntax as nullable value types. So if we want to properly delcare the string s from before, declaring the intention that it can be null we can use the following:
string? s = null;
This is a breaking change though, so what will the compiler do for existing code? Answer: the compiler will just give a warning when null is assigned to a reference type that is not nullable (so the first example given).

Async Streams

Additional work is being done with asynchronous methods in C#8, specifically combining async with iterator methods to create async streams. This means that we should be getting an async version of IEnumerable - IAsyncEnumerable<T> where move next is an async operator. So we will be able to write the following:
foreach await (var s in stream)   
{   
     // awaits for each next  
}  

Default Interface Implementations

Suppose that we have the following interface and implementation:
public interface INotify   
{  
     void Notify();  
}  
public class C : INotify  
{  
     public void Notify()  
     {  
          ...  
     }  
}  
But later we wanted to a new method to INotify, we can't because this will break the contract will all existing implmentations.

Default implementations will let you define an implementation in the interface, then all exisitng implementations will use this default implementation if they do not implement their own version. So we could update INotify to the following:
public interface INotify 
{
     void Notify();
     void NotifyAll(IEnumerable<string> strs) { foreach (var s in strs) { ... } }
}
This does mean that interfaces are crossing the boundaries with abstract classes, but implementations keep the difference with abstract classes that one class can implement multiple interfaces.

Extensions

New syntax for extension methods is planned for C#8, simplifying the syntax used to define extension methods and to allow extension properties to be defined. Under the proposed changes, the new extension type declaration would be used:
public class Person
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

extension Employee extends Person
{
     public int EmployeeId { get; set; } // extension property
 
     public string GetEmployeeName() // extension method
     {
          return string.format("{0} {1}", this.FirstName, this.LastName);
     }
}
As shown in the above example, under the proposed changes, the first parameter of the extension method no longer needs to be this but this will still be accessible within the method.

The End

Comments

Popular posts from this blog

My First Year as a Data Scientist

PIVOT and UNPIVOT in T-SQL

My First Retro