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).
After last update it doesn’t work anymore
LikeLike
I haven’t updated to the very latest VS build. But I will try it out shortly and see if there’s some differences. Thanks.
LikeLike
I just tried this on the latest VS build (17.12.5). Everything still works the same for me.
LikeLike
The latest version of Visual Studio is 17.13.0. Before this version it’s worked correctly
LikeLike
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?
LikeLike
I tried this on three different computers (all with windows 10).
LikeLike
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…
LikeLike
I tried this with all variant of dropdown – Error, warning, Refactoring Only et al. and it doesn’t affect anything.
LikeLike
That is interesting… I had no problem with my upgrade.
Do you have an .editorconfig file saved somewhere in your project/solution?
LikeLike
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.
LikeLike
I finally solved this problem by changing class and interface template files.
LikeLike