Introduction to C# 10 - Part 1

02 February 2022 at 10:00 by ParTech Media - Post a comment

Technologies keep evolving and provide new features/upgrades over the existing ones with better user-friendliness and better performance. Some of the upgrades are used daily while some rarely but they still provide effectiveness.

In the same manner, Microsoft keeps releasing a new version of the:

  • .Net framework
  • A programming language that can be used to develop applications via the framework
  • An IDE that can help in the development

A framework typically gets updates related to how things get processed and overall improvements in platform-oriented tasks, SDK-related upgrades, and so on.

On the other hand, a programming language gets upgrades to make things simpler for programmers.

In this post, let’s see some of the cool new features introduced for C# 10, a really popular programming language.

Tables of contents

  1. How to install and use C#10
  2. Features in C# 10
  3. Conclusion

How to install and use C# 10?

C# 10 comes with the .Net 6 SDK package. It is mandatory to use this. But how do we make use of it? Simple, we need an IDE like Visual Studio. However, there is a catch here. In Visual Studio 2019 (which is the predecessor of Visual Studio 2022), C# 10 can be consumed only in Mac systems.

On Windows machines, Visual Studio 2022 is needed. Don’t forget - while installing, you need to select .NET 6 feature to enjoy all the latest features including IntelliSense and other smart tools that support C# 10 programming language.

Features in C# 10

Firstly, Visual Studio 2022 comes with a new look and feel. To understand C# 10 features better in it, let us try creating a console application and implement these features.

The folder structure of a Console application looks similar in VS 2022 and VS 2019. But the content inside the Program.cs file looks different (without the header and Main method of the class). This is something similar to what we saw in one of our previous blogs on Top-level programming in C#.

Global using and Implicit using

In all the class files, there will be a header section where the references are being added for the classes that are consumed in the same file. However, now consider a web API. In the web project, there are multiple controllers defined. Each of the controller classes has a reference with the same business project. This simply means that the same project is referred to in multiple controller files.

This has been simplified in C# 10 by introducing global using statements. We have added a COM reference to our project ‘Microsoft Excel’. In the program.cs file,

global using Microsoft.Office.Interop.Excel;

is added, and in the main method a sample code -

Application app = new Application();  

is added.

Now, a new class Parallel.cs is created and inside which if we add

Application app = new Application();

without any header section in this file, Excel namespace class Application can be accessed. Even alias namespaces can also be declared globally. For example,

global using excel = Microsoft.Office.Interop.Excel;

Also, if you feel some of the implicit usings are not required, then they can be removed by adding the below set of lines in the project file.

<Using Remove="System.Linq" />

This will make the users add the respective reference to the class file manually.

File-scope namespace

Classes are created under a namespace and for consuming the class, the namespace has been referred to in the header of the file.

While creating a class, the structure of the file goes like this -

header content (using statements)
Namespace name
​    	Public class NewClass
​    	{
​    	}

With C# 10, the nesting of classes inside a namespace can be avoided. Instead, a namespace can be used as a using statement.

namespace PARTECH_C10;
public class Parallel
  Application app = new Application();

This indicates that the Parallel class falls under the PARTECH_C10 namespace. And only one file-scoped namespace is allowed and it should be at the top before any declarations.

Extended property patterns

C# 10 provides options to access the properties of the extended class in an easier way which can be used in pattern matching. In other words, it lets you access properties of a class that are referred to inside another class. In the below example, we are accessing the Line1 property during pattern matching.

public class Person
​    public string Name { get; set; }
​    public Address AddressInfo { get; set; }

public class Address
​    public string Line1 { get; set; }

Consider a class Person which has a nested class - (Address) object being referred inside it. Now the Person object and the Address object inside Person have values. If we need to pattern match an object with Address object property, then before C# 10, we had to do it in the below way,

Public class Main()
var obj = new Person 
​      { 
​        AddressInfo = new Address { Line1 = "First Line"}
​      };

​      if (obj is Person { AddressInfo: { Line1: "tst" } })
​      {
​      } 

With C# 10, the same comparison can be done by -

if (obj is Person { AddressInfo.Line1: "tst" })

​    {
​    }

It provides the option to directly access the properties inside the Address class with the property inside Person class and do pattern matching. This makes the code looks simpler and easier to understand.


All these features of C# 10 come in handy for developers while developing applications. There are several other key features related to lambda, methods, strings, etc. which we haven’t covered in this blog. We will look into them in detail in Part 2 of the blog.