What's new in C# 10 ( .NET 6)

What's new in C# 10 ( .NET 6)

In this article we will be discussing about few features of upcoming C# 10.

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 (github.com/dotnet/csharplang/milestones) , you can find a lot of features and ideas that are suggested for the new version of the language.

Here are five new features that we will see in the new version of C#10:

1. Field Keyword

2. Required Properties

3. Null Parameter Checking

4. File Namespaces

5. Global Usings

Of course, these 5 confirmed features aren’t all we’ll see in C# 10. There are still chance that we’ll get more work around expressions and a controversial change that will let us define static members in an interface. Lets discuss one by one and see how it will improve the coding standard or what is the benefit of using these new features in C# 10 .

1. Field Keyword

To understand this , let’s take an example of Employee class as

public record Employee
{
public string Name { get; init; }
public int Age {get;init;}
public DateTime DateOfJoining { get; init; }
}

Here Employee class has three immutable property using the get and the init keywords. In background , three private fields are created and data is stored in them . So ultimately Employee class will be like

public record Employee
{
private string _Name ;
private int _Age;
private DateTime _DateOfJoining;

public string Name { get {return _Name;}; set {_Name=value}; }
public int Age {get {return _Age;};set {_Age=value};}
public DateTime DateOfJoining { get{return _DateOfJoining;}; set{_DateOfJoining=value;}; }
}

But in C# 10, there’s a new backdoor with the field keyword, which exposes the automatically created backing field. So instead of creating private field behind you can directly access it using field keyword

For example, Here’s a revision of the Employee class that makes sure the DateOfJoining field only contains the date information from the DateTime object, and no time information:

public record Employee
{
public string Name { get; init; }
public int Age {get;init;}
public DateTime DateOfJoining { get; init => field = value.Date();  }
}

This code seems clean , simple and declarative.

You can use the field keyword to access the backing field in a get, set, or init procedure. As long as you don’t change the data type of your property, there’s no longer a need to declare the backing field yourself.

Existing Employee class as below

public record Employee
{
private string _Name ;
private int _Age;
private DateTime _DateOfJoining;

public string Name { get {return _Name;}; set {_Name=value}; }
public int Age {get {return _Age;};set {_Age=value};}
public DateTime DateOfJoining { get{return _DateOfJoining;}; set{_DateOfJoining=value;}; }
}

by using field keyword , above code is reduced to

public record Employee
{

public string Name { get; set {field =value}; }
public int Age {get ;set {field=value};}
public DateTime DateOfJoining {get; set{field =value;}; }
}

2. Required Properties

Now suppose we want to create an object of Employee class mentioned above using c#, we can simply create as

Employee emp=new Employee
{
Name=”Dhiraj Kumar”,
Age=”37”,
DateOfJoining=DateTime.Now
};

But lets take a scenario where we need any field must be initialized while creating the object and that is not handled here. For handling the situation of initializing few required field/property of class, C#10 required keyword is introduced. So suppose we have to declare the Employee class where while creating object of Employee class, Name must be given . Lets take the example of modified Employee class

public record Employee
{
public required string Name { get; init; }
public int Age { get; init; }
public DateTime DateOfJoining{ get; init; }
}

Now the compiler won’t allow you write code that creates an Employee but doesn’t set the Name property. So we cant be able to create object like

Employee emp =new Employee 
{
Age=37,
DateOfJoining=DateTime.Now
};

3. Null Parameter Checking

C#10 has introduced a new feature of null checking. Lets take an example where we want to write a method for updating DateOfJoining of Employee class. It takes DateTime and Employee class object.

public UpdateDateOfJoining(DateTime newDate, Employee emp)
{
    if (emp == null)
    {
        throw new ArgumentNullException("Emp");
    }    
 emp.DateOfJoining=newDate;
}

Here we have to check whether passed Employee object is null or not . If we assign the value without checking null value , then it will automatically throw an exception .

For handling this C#10 introduced !! that can be passed after any object to check the null value of object. !! could be at the end of passed parameter. So method will be now

public UpdateDateOfJoining(DateTime newDate, Employee emp!!)
{
 emp.DateOfJoining=newDate;
}

Now if you pass a null value in the place of an Employee object, the ArgumentNullException is thrown automatically. It gives developer facility to write less code and handle null exception automatically.

4. File Namespaces

In C# 10 provides facility to declare a file-scoped namespace for your code. The file-scoped namespace applies automatically to your entire file, with no need to indent anything. So , previously you write code like

Namespace C#10Features
{
   Class FileScope
   {
   }
}

In C# 10 , it becomes

Namespace C#10Features
Class FileScope
   {
   }

If you add a namespace block to a file that uses a file-scoped namespace, it creates a nested namespace, as you would expect:

Namespace C#10Features
Namespace NewFeatures
{
Class FileScope
   {
   }
}

This block creates namespace C#10Features.NewFeatures.

The C# designers describe this change as cleaning up horizontal waste. The overall goal is shorter, narrower, more concise code expression.

5. Global Usings

Let’s create one project of Asp.net MVC (Core) and you have seen that multiple namespace are already imported which is required for running the code. These namespaces exist in all related files and created duplicate. For removing these , C# 10 added new features where you can put all common namespace in one place and it will automatically included in other files.

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;
using MyApp.Data;
using MyApp;


namespace Employee
{
    public class Startup
    {
        ...
    }
}

C# 10 introduces a new pattern that lets you define namespace imports across an entire project using the global keyword. It’s recommended that you place your global imports in a separate file (one for each project), possibly named usings.cs or imports.cs.

Here’s what that can hold:

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;

Now you can simplify the original file:

using MyApp.Data;
using MyApp;
namespace Employee
{
    public class Startup
    {
        ...
    }
}

Visual Studio highlights namespaces that are duplicated (imported globally and locally in a file). Although that’s not an error, removing duplicate namespaces allows you to reduce the amount of stock code and focus attention on the special namespaces that a particular file is using.

That's all for this Blog developers and with that, it's a wrap! I hope you found the article useful.

I create content about Programming, and Productivity, If this is something that interests you, please share the article with your friends and connections.

Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

I would strongly recommend you to Check out my YouTube Channel youtube.com/c/codeasitis where i post programming video and don't forget to subscribe to my Channel. I would love to connect with you at Twitter ( twitter.com/codeasitis1 ) | Instagram ( instagram.com/codeasitis )

See you in my next Blog article, Take care!!