In this article we’ll discuss What Is New in C# 10? There are lot more additions, but we have picked few selective ones and believe these will be a value addition to your knowledge. Let’s check below:

1. Global ‘using’ directives

You can add the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation. You will add this in one file like “common.cs” and this will be typically available to all source files in a project. This feature will help in reducing the size of the code files and there will be no reddened code in the project. 

Using directive in C# 9 or earlier.

using System; 
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;

As per new feature of C# 10

global using System;
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;

2. 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. Finally we can say that It has shorten the syntax.

“File Namespace” in C# 9 or earlier

namespace CodeConfig
{
  public class Article
  {
    ...
  }
}

As per new feature of C# 10

namespace CodeConfig;

public class Article
{
  ...
}

3. Null Parameter Checking

Null Reference Exception is one of the worst bugs that you can have in your code. It is really 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.

Null check implementation in C# 9 or earlier.

namespace CodeConfig;

public class Article
{
public PostArticle(int postId, ContentClass ctObject)
{
  if (ctObject == null)
  {
    throw new ArgumentNullException("ctObject ");
  }
  ...
}
}

Null check as per new feature of C# 10

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.

namespace CodeConfig;
public class Article
{
   public PostArticle(int postId, ContentClass ctObject!!)
   {
     // write your code here
   }
}

4.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. Record is primarily intended for supporting immutable data models.

Example of ‘Record’ in C# 9

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

public record User(string FirstName, string LastName);

Example of ‘Record’ in C# 10 with Struct

In C#10, records are taken one step further. 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 User(string FirstName, string LastName);

5.Extended property patterns

Beginning with C# 10, you can reference nested properties or fields within a property pattern. For example, a pattern of the form in which nested property can be accessed using (.) dot operator. Below syntax is valid in C# 10 and later and equivalent to

{ Prop1.Prop2: pattern }

Prior to C# 10 the same was feasible using the below syntax. 

{ Prop1: { Prop2: pattern } }

6.Assignment and declaration in same deconstruction

This change removes a restriction from earlier versions of C#. Previously, a deconstruction could assign all values to existing variables, or initialize newly declared variables:

// Initialization:
(int x, int y) = point;

// assignment:
int x1 = 0;
int y1 = 0;
(x1, y1) = point;

C# 10 removes this restriction as shown in below example:

int x = 0;
(x, int y) = point;

I hope with this article you have learned the new features of C# 10 (in VS 2019) and you will use these in your daily development practices.

I hope you have enjoyed the reading of above article. For knowledge of ADO.NET interview questions, you can check this section HERE. You can learn more on C# from Microsoft Learn.

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can