Subscribe and receive free guide - Ultimate Data Visualization Guide

* indicates required

C# has been around for a while. Since January 2002, to be more precise. As we wait for the 20th birthday of this popular programming language, we are expecting its new version as well. C# 10 doesn’t have an official release date yet, although it will probably be released in November along with the .NET 6. If you follow its GitHub page, you can find a lot of features and ideas that are suggested for the new version of the language.

Ultimate Guide to Machine Learning with Python

This bundle of e-books is specially crafted for beginners.
Everything from Python basics to the deployment of Machine Learning algorithms to production in one place.
Become a Machine Learning Superhero 
TODAY!

This can be a bit overwhelming, however, if you follow Microsoft conferences and listen to what some of the C# designers are saying and cross-check it with suggestions on the Github page, you can get a nice picture of what is coming next for C#. For example, you can check out this Microsoft Build conference from May and you can find a lot of interesting information. Here are five new features that we will see in the new version of C#:

  1. Null Parameter Checking
  2. Required Properties
  3. Field Keyword
  4. Global Usings
  5. File Namespaces

1. Null Parameter Checking

Null Reference Exception is one of the worst bugs that you can have in your code. It is just painful. In order to protect from this type of exception, you need a bulletproof design of the application and a lot of checks for parameters of the functions. This new feature should help us with that and make our code more readable and robust.

Recommendation Systems

1.1 Current Problem

At the moment if you want to check a certain parameter of your function is null, you would have to do something like this:

public SomeFunction(int id, SomeClass newObject)
{
  if (newObject == null)
  {
    throw new ArgumentNullException("newObject");
  }
  ...
}

In the code above, we inject newObject of SomeClass into SomeFunction. If the mentioned object is null we should not proceed with the code. I know a lot of people will say that with the correct application design this problem should not exist in the first place, however, sometimes it is necessary to do this.

1.2 C# 10 Feature

The new version of C# aims to simplify this problem for us with – !!. All you have to do is add two exclamation points after the name of the parameter:

public SomeFunction(int id, SomeClass newObject!!)
{
	...
}

In this example, the code will automatically check if newObject is null. The ArgumentNullException will be automatically thrown if the newObject value is null.

2. Required Properties

Is it only me or the constructors were more explicitly used in the past? Today we don’t write heavy constructors and more often than not we use lightweight constructors, meaning we define the values for the properties directly.

Programming

2.1 Current Problem

For a couple of versions of C# now, we can create objects using lightweight constructors like this:

var newArticle = new Article
{
	Title = "C# 10 - Top 5 new features",
	Category = ".NET",
	ReleaseDate = DateTime.Now()
}

The main problem with this approach is that sometimes we need to initialize some property and we don’t do that. For example, we might do something like this, even though it doesn’t make sense to create an Article object without a title:

2.2 C# 10 Feature

C#10 introduces a new keyword for properties – required. This way when defining a class, we can make a property required. If you try to make an object without the required property you will get an error in compile time.

public class Article
{
  public required string Title { get; init; }
  public string Category { get; init; }
  public DateTime ReleaseDate{ get; init; }
}

3. Field Keyword

Auto implemented properties are not a new feature, in fact, they first appeared in C# 3. This means that when the code is compiled compiler creates a private, anonymous backing field that can only be accessed through the property’s get and set accessors.

One of the cool features that came with the current version of C# is an init accessors. With this accessor, immutable objects are more flexible, and they allow the caller to mutate the members during the act of construction.

Computer

3.1 Current Problem

This is all great, however, when this approach doesn’t suit your problem, you’re forced to add the backing field and write the usual property methods. Something like this:

public class Article
{
  private string _title;
  public string Title
  {
    get
    {
      return _title;
    }
    set
    {
      if (value.Trim() == "")
        throw new ArgumentException("Title can't be empty");

      _title = value;
    }
  }
}

3.2 C# 10 Feature

The new version of the C# programming language introduces another new keyword – field. Code from above can now be simplified:

public class Article
{
  public string Title
  {
    get;
    set
    {
      if (value.Trim() == "")
        throw new ArgumentException("Title can't be empty");
      field = value;
    }
  }
}

In an essence, from C# 10 there will be no need to implement backing field on your own. This keyword can also be used in the init accessor:

public class Article
{
  public string Title
  {
    get;
    set
    {
      if (value.Trim() == "")
        throw new ArgumentException("Title can't be empty");
      field = value;
    }
  }

  public string Category { get; init; }
  public DateTime ReleaseDate{ get; init => field = value.Date(); }
}

4. Global Usings

This is a new feature that will simplify the code a lot. As you are aware, every C# file starts with a list of usings that are necessary for the implementation. However, sometimes this is redundant. Especially if you are working with ASP.NET a lot of that code is repeated and in general just a noise for coders.

Computer

4.1 Current Problem

If we take for example one ASP.NET file, the list of usings is huge:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

4.2 C# 10 Feature

Another keyword that C# 10 introduces is global. Using global, you will be able to define global usings for the whole project. In general, it is recommended to create a separate file which will contain these imports, something like usings.cs.

This means that other files in the project can be simplified since they no longer need to add all these usings. C# designers refer to this as the elimination of vertical waste.

global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.HttpsPolicy;
global using Microsoft.AspNetCore.Identity;
global using Microsoft.AspNetCore.Identity.UI;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;

5. File Namespaces

Using the global keyword C# eliminates vertical waste and with file namespaces it eliminates horizontal waste. Basically, this feature aims to solve the indentation problem and it is more of a cosmetic change that will hopefully make your code cleaner.

Partial

5.1 Current Problem

Nowadays, after the list of usings, the first thing you see in a C# file is the definition of the namespace. After that comes the definition of the class. The implementation of the class is already indented at this point. This is what we consider horizontal waste.

namespace Blog
{
  public class Article
  {
    ...
  }
}

5.2 C# 10 Feature

With this feature you can define the namespace on the file level like this:

namespace Blog;

public class Article
{
  ...
}

Conclusion

In this article, we had a chance to get familiar with 5 new features that the new version of C# will bring. To be honest I can’t wait to try them all out. Some of them seem like a major improvement and it seems that it will affect how we organize and write C# projects.

Thank you for reading!

Nikola M. Zivkovic

Nikola M. Zivkovic

Nikola is the author of books: Ultimate Guide to Machine Learning and Deep Learning for Programmers. He loves knowledge sharing, and he is an experienced speaker. You can find him speaking at meetups, conferences, and as a guest lecturer at the University of Novi Sad.

Ultimate Guide to Machine Learning with Python

Everything from Python basics to the deployment of Machine Learning algorithms to production in one place.

Become a Machine Learning Superhero TODAY!