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 got its new version as well. C# 10 is released in 9. 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. Extended Proptery Patterns
  3. Record struct
  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. Extended property patterns

Sometiemes we need nested properties in our code. Meaning property of a property. Using pattern matching on these values is posible in previous versions of the C# as well, but the code is kinda ugly.

Programming

2.1 Current Problem

Here is what code for switch looks like in C#9:

if (e is MethodCallExpression { Method: { Name: "MethodName" } })

Bah, a bit wierd. It would be easier to just use the dot in patter matching. 

2.2 C# 10 Feature

C#10 introduces exatcly the improvement that is necessary. Instead of repeating brackets several times, now the code from above is more intuitieve:

if (e is MethodCallExpression { Method.Name: "MethodName" })

3. Record Struct

Record keyword for classes was first introduced in C#9. This keyword is used for reference types that provide built-in functionality for encapsulating data. Records are primarily intended for supporting immutable data models. 

Computer

3.1 Current Problem

In previous version of C#, this keyword was reserved for classes. 

public record Person(string FirstName, string LastName);

3.2 C# 10 Feature

In C#10, records are taken one step fruther. Now, you can also use structure types to design data-centric types that provide value equality and little or no behavior. In C# 10 and later, you can define record struct types:

public readonly record struct Point(double X, double Y, double Z);

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.

Discover more from Rubix Code

Subscribe now to keep reading and get access to the full archive.

Continue reading