Four features in C# 9 that aren't in C# 8
Published: Tuesday 29 September 2020
We will try out four new features in C# 9 that were not in C# 8.
Feature #1: Target-Typed Object Creation
There is a new way where we can create a new instance of a particular type. Rather than explicitly calling the type after the 'new' expression, we can now create a variable with that type, and simply use the 'new' expression.
class Program
{
static void Main(string[] args)
{
Product myProduct = new Product();
}
}
public class Product
{
public string Name { get; set; }
public string Sku { get; set; }
}
However, this can be simplified in C# 9 by just calling the new keyword, wrapped with brackets:
Product myProduct = new();
And it works if we have multiple constructors. So if we decide we want to pass in the Name and Sku into the constructor, we can do that as well.
class Program
{
static void Main(string[] args)
{
Product myProduct = new("My Product", "PRODUCT-001");
}
}
public class Product
{
public Product()
{
}
public Product(string name, string sku)
{
Name = name;
Sku = sku;
}
public string Name { get; set; }
public string Sku { get; set; }
}
Feature #2: Init-Only Setters in Properties
Whilst we are on the subject of initialisation, we can now set a property setter as "init". What that means is that we can only set the property when an instance of the type is created.
public class Product
{
...
public string Name { get; init; }
...
}
In the past, if we wanted to set a property outside the class, we would have to make the property publicly available to be able to be set.
class Program
{
static void Main(string[] args)
{
Product myProduct = new Product { Name = "My Product", Sku = "SKU-001 " };
myProduct.Name = "My Product 2"; // This would throw an error
}
}
public class Product
{
public Product()
{
}
public string Name { get; init; }
public string Sku { get; init; }
}
Feature #3: Covariant Returns
Covariant returns is where we can override a method and return a type that inherits the original type.
public class Product
{
public Product()
{
}
public string Name { get; init; }
public string Sku { get; init; }
}
public class CameraProduct : Product
{
public string TypeOfCamera { get; set; }
}
public abstract class ProductService
{
public abstract Product GetProduct();
}
public class CameraProductService : ProductService
{
public override CameraProduct GetProduct()
{
return new CameraProduct();
}
}
Take the example above. We have a CameraProduct class that inherits the Product class.
Feature #4: Relational & Logical Patterns
Switch patterns have made a significant change in C# 9.
static double Postage(double price) => price switch
{
< 20 => 5.99,
>= 20 and < 40 => 3.99,
>= 40 and < 60 => 2.99,
_ => 0
};
Bonus Feature: Extending Partial Methods
We are going to look at one more feature. This is where we can extend partial methods.
In addition, the video will demonstrate and cover the four other C# methods we have covered in this article.
More C# Features To Come
There are more features for C# 9 to come.
Related articles
Write cleaner code in C# with changes you may not know about
We'll take a look at the C# features and improvements over the years that help you to write cleaner code and improve it's readability.