Home > other >  Visual Studio 2022 Professional New Class Code Template
Visual Studio 2022 Professional New Class Code Template

Time:11-10

I want VS to generate new classes using the new way of declaring namespaces, ie:

namespace A.Namespace.For.Class;
public class ANewClass
{
}

But my VS2022 still uses the old way, ie:

namespace A.Namespace.For.Class
{
    public class ANewClass
    {
    }
}

So I tried to change the template file located at C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

I changed it to the below:

namespace $rootnamespace$;
public class $safeitemrootname$
{
}

But it looks like VS2022 can't interpret the semicolon after the namespace's name because it still uses the old style. But as soon as I remove the semicolon, VS starts to use the new style except that I now have to manually add that semicolon myself.

Anyone knows how to escape that semicolon in that code template file? No documentation seems to be available.

CodePudding user response:

You don't have to change the template, this is configurable, go to

Options > Text Editor > C# > Code Style > General

In Code block preferences section, change Namespace declarations to File scoped.

  • Related