简体   繁体   中英

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

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

Eg 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?

在此处输入图像描述 在此处输入图像描述

UPDATE

I can not find the RootNamespace in my project:

在此处输入图像描述

So, the provided answer has no value for me.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM