How can I use a file-scoped namespace declaration in a class template?

asked4 years ago
last updated4 years ago
viewed4.4k times
Up Vote13Down Vote

C# 10 introduced file-scoped namespaces, which I would like to use in Visual Studio's class templates. I've updated the 'Class' template file to the following:

namespace $rootnamespace$;
class $safeitemrootname$
{
    //I put this comment here to make sure it's using the right file
}

But when I create a new empty class I get this autogenerated code:

namespace ProjectName
{
    internal class Class1
    {
        //I put this comment here to make sure it's using the right file
    }
}
namespace ProjectName;  
internal class Class1
{

}

For reference, I am using Visual Studio 2022 Professional and my project is using C#10 with .NET 6. The location of the class template file that I am modifying is: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs