How can I configure default VS Code namespaces for C#?

Viewed 1219

How can I configure default VS Code namespaces for C#?

E.g. I have the following folders structure:

gameplay/input/controllers/foo.cs

I created the foo.cs by right clicking the controllers folder and selecting New C# Class. And here is how the class looks by default without any changes from my side:

namespace play.input.controllers
{
    public class foo
    {
        
    }
}

While I would expect the namespace in this case to be gameplay.input.controllers. I am confused why would VS Code change the gameplay to play? How could I fix it?

enter image description here enter image description here

UPDATE

I can not find the RootNamespace in my project:

enter image description here

So, the provided answer has no value for me.

1 Answers

You can set the default namespace of a project in Project Preferences->Application. The string that is visible in the "Default Namespace" input box is used for new files in the root directory of the project. If you create items in subfolders, these will be included in the namespace by default.

If you want to remove the application name (play in your case), you can define the default namespace as empty.

You can also edit the project file to include the RootNamespace tag, as follows:

  <PropertyGroup>
    <TargetFrameworks>net5.0;netcoreapp2.1</TargetFrameworks>
    <EnableDefaultItems>false</EnableDefaultItems>
....
    <RootNamespace>MyRootNamespace</RootNamespace>
....
    
  </PropertyGroup>
Related