In C# version 10, support for file scoped namespaces was added. This allows us to eliminate more boilerplate from our classes and also get rid of some extra unneeded identation. Unfortunately, when you create a new C# class in Visual Studio 2022, the template still uses the old namespace declaration style. I don’t like that, and if you found this blogpost, neither do you!

Thankfully, changing this is not hard.

First you need to navigate to:

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

There you will find a Class.cs that looks something like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
namespace $rootnamespace$
    class $safeitemrootname$

Now you can change this to whatever you want (you might need to open your text editor in admin mode to make changes to this file). I personally changed it to:

namespace $rootnamespace$;

public class $safeitemrootname$

Because I’m using global imports, I want to eliminate as much boilerplate as I can from my new classes. Since this is a global setting you can wrap it around an $if$ ($targetframeworkversion$) so that you only get a file scoped namespace class generated if you are at framework version 7 or higher.

Finally Visual Studio will auto format our new and beautiful file scoped namespace to the old style namespace. We can prevent that by adding a .editorconfig at the root of our solution containing the following line:


Now, whenever we create a new class, it will be formatted with the file scoped namespace by default! Good luck and have fun with this!