Configure Visual Studio to Use File-Scoped Namespaces on New Classes

Filed-scoped namespaces were a new feature added to C# 10. File-scoped namespaces simplify the organization of code by allowing you to declare the namespace at the file level rather than wrapping each type in its own namespace block. This can lead to more readable and concise code with one less layer of curly braces in your code.

namespace MyNamespace;

public class MyClass
{
    public void MethodInClass()
    {
        Console.WriteLine("Method in MyClass");
    }
}

Regardless, when you add a new class to your project, Visual Studio still defaults to creating that class with a block-scoped namespace. And you have to change that scoping manually. However, there is a VS setting that allows you to change that default behavior.

In your Visual Studio:

  • Launch the Tools > Options dialog.
  • Navigate to Text Editor > C# > Code Style > General on the left tree panel.
  • The, scroll down to the Code Block preferences and the Namespace declarations specifically.
  • Change the setting dropdown from Block scoped to File Scoped.
  • And save the changes by clicking the Ok button.

Now, if you add a new class file to your project, it will be generated with file-scoped namespaces. This is great timesaver if you’re using the latest C# language features.

Note: The above changes this preference globally for all projects, and it isn’t shared among developers on your team. You can also make this settings change in an .editorconfig file that can be shared via source control and scoped to the open solution.

# Code-block preferences
csharp_prefer_braces = true
csharp_prefer_simple_using_statement = true
csharp_style_namespace_declarations = file_scoped
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_primary_constructors = true
csharp_style_prefer_top_level_statements = true

In conclusion, file-scoped namespaces are a new language feature to help remove some clutter from you C# files. And the namespace declaration settings can configure Visual Studio to generate new class files in that style (rather than having to change your new class files manually).

11 thoughts on “Configure Visual Studio to Use File-Scoped Namespaces on New Classes

      1. That is very strange. I just upgraded to 17.13.0, and the file-scoped namespaces for new classes is still working as expected for me. I wonder if there was some other configuration clash that caused the error you saw?

        Like

      2. In the dropdown with the ‘Error’ selection, change that entry to ‘Refactoring Only’. And then creating a new class should work as expected.
        This is what my setting looked like after the VS update…

        Like

      3. I tried this with all variant of dropdown – Error, warning, Refactoring Only et al. and it doesn’t affect anything.

        Like

      4. I tried different variant – with .editorconfig file and without. But i don’t configure namespace in this file. All my preference i set in visual studio only.

        Like

Leave a reply to DarthPedro Cancel reply