.NET 6 New Features Trial | Namespace of File Range

Foreword

In the previous "implicit Using Directive" article, we introduce global using, which can reduce "noise" in our code file to make the code more concise.

Today, we will introduce another way to simplify the code in. Net 6 – Namespaces of the file range.

Demo

When we add a class file to the project, the format that defines the namespace where our code is located is like this:

Namespace xxxnamespaceename

{

Public class xxxclassname

{

}

}

We already have a lot of "noise", such as {} and indentation before no code.

Now, you can change the namespace of the file range to simplify the code:

Namespace xxxnamespaceename;

Public class xxxclassname

{

}

It can be seen that a semicolon is added after the namespace indicates all types in this file, such as classes, and interfaces in this namespace.

Modify code style

Currently, the VS declaration namespace defaults or use the old style, if you like the namespace of the file range, you can modify it to the default.

Open .editorconfig, set the "Namespace Statement" to "File Scope Limit":

Try to create a class file again, you will find namespaces that have been used is the namespace of the file.

in conclusion

It should be noted that each file can only declare a namespace of a file range. Usually, most C # files have only one namespace, so this is not a problem.

If you think this article is inspired by you, please help you.greatorlook in