SHARE:

Introducing C# 10: File-scoped namespaces

Introduction

C# is still evolving and C# 10 proposes a new feature : file_scoped namespaces. As its name suggests, the namespace declared in a file (without braces, but whose instruction ends with a semicolon) will apply to all elements declared in the same file. It’s practical, no more need for indentation since the opening and closing braces of the namespace disappear. There is still a limitation, only one namespace can be declared in the file, otherwise you will get a compiler error.

Example

The following code shows what is a file-scoped namespace:

The following below is illegal:

The compiler will throw the following error:

Error CS8955 Source file can not contain both file-scoped and normal namespace declarations.

The following below is also illegal:

The compiler will throw the following error:

Error CS8954 Source file can only contain one file-scoped namespace declaration.

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: