How to set C# 10+ file scoped namespaces as default in Visual Studio

If you’re using Visual Studio 2022 & want to use File-scoped namespaces (C# 10+) by default when you create a new class set the ‘Namespace declarations’ preference to ‘File scoped’ as shown below …

Note however these are local user settings.

When in a team environment it is better to use an EditorConfig file to enforce settings across an entire solution or project for all devs.

You can generate an .editorconfig file from the settings above using the button highlighted in blue.

The relevant line if you just want file scoped namespaces as a preference is …

csharp_style_namespace_declarations = file_scoped

but if you actually want Visual Studio to cause compile time errors if you don’t use file-scoped namespaces the syntax would be …

csharp_style_namespace_declarations = file_scoped:error

Leave a Reply

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