Skip to content

Change default namespace associated to a folder when creating new class #5372

Description

@vsfeedback

This issue has a corresponding ticket on Developer Community. Please vote and comment there to make sure your voice is heard.


It would be nice if there is a feature to set a default namespace for each folder when creating a new class. For example, if we have the following file structure:

MyProject
     MyFolder
          MyClass1

Then if we create MyClass2 under MyFolder, the created class will be under the MyProject namespace instead of MyProject.MyFolder.

Additional references: https://stackoverflow.com/questions/1317901/change-default-namespace-when-creating-class-in-folder-visual-studio

Activity

  1. drewnoakes commented on Aug 26, 2019

    @drewnoakes
    Member

    To paraphrase, the request is to be able to mark a folder with metadata to stop it from participating in namespace construction.

  2. davkean commented on Aug 27, 2019

    @davkean
    Member

    Yep interesting and reasonable request. We don't have the ability to associate metadata with a folder in CPS - other than empty folders, they don't have a item representation, so we'd need to introduce that.

  3. added this to the Backlog milestone on Aug 27, 2019
  4. JimF42 commented on Oct 4, 2019

    @JimF42

    @davkean You might want to look at this feedback post I created. Admittedly, this was before I realized that ReSharper was doing part of this...
    https://developercommunity.visualstudio.com/content/problem/621033/adding-a-class-does-not-honor-namespace-provider-s.html

    So somehow ReSharper is keeping meta data on the folder to determine if the folder name should be part of the namespace. If I add a new class via ReSharper (which I never do) it's honored. If I use VS to add a class, it's not honored (not surprisingly in hindsight).

    Here is a small screenshot of a folder being selected and ReSharper's added property "Namespace Provider"

    Folder Properties

    Now, I am not expecting you to code to ReSharper's standard, but when you start working on this you may let them know so they can take it into account in their code. I would hate to see double folder properties and have to set them both not knowing which one is which.

  5. drewnoakes commented on Oct 6, 2019

    @drewnoakes
    Member

    ReSharper stores this data in their own resource file, outside of the project file. We would want to store it in the project file directly.

    For example:

    <ItemGroup>
      <Folder Include="Models\" IsNamespaceProvider="False" />
    </ItemGroup>

    Currently this Folder item is removed as soon as a class is added to that folder. We'd need to ensure Folder items with custom metadata are not removed in such cases.

  6. mrpmorris commented on Jan 1, 2020

    @mrpmorris

    This is useful when we want to add all of our class extensions in an Extensions folder but want them all to be in the main namespace.

    I think the best option would be to allow us to bring up the Properties window and edit a DefaultNameSpace property on the folder.

  7. added
    Triage-Approved
    on Jan 17, 2020
  8. davkean commented on Jan 17, 2020

    @davkean
    Member

    @jinujoseph Are you tracking anything similar on your side?

  9. self-assigned this
    on May 5, 2020
  10. Codentale commented on Jun 15, 2020

    @Codentale

    This is useful when we want to add all of our class extensions in an Extensions folder but want them all to be in the main namespace.

    Agree, this is quite common case when some folder need to be in parent namespace.
    My thought:

    1. Create Virtual Folder in project (like solution virtual folder) with unique icon.
    2. Or ignore folder with special prefix like maybe @Extensions or _Extensions.
      both way are very clear to indicate all items in the folder are using parent namespace, which I think is better then ReSharper way.
  11. thomaslevesque commented on Oct 1, 2020

    @thomaslevesque
    Member

    Any news on this? Is it being considered for a future update?

  12. CodeAngry commented on Oct 15, 2020

    @CodeAngry

    This would be really useful!

    Adding metadata to any C# Project folder to override default folder-structure based namespace with:

    • default namespace: (empty)
      would follow default functionality
    • absolute namespace: /Parent/Child
      allows full namespace override
    • relative namespace: Child or ./Child or ../../Child
      that would resolve based on parent Namespace (which obviously can be overridden itself).
    • no namespace: -
      this would use parent namespace without child participating in the name construction

    And any file added to that folder will have the namespace based on the override when file is created. And this should only apply to new files. If you change the folder's virtual namespace, there should be no sync for existing files with the old namespace. That's the developer's job to rename.

    This would really help because often, you find yourself wanting to structure (1 or multiple folders deep) files but need them all in the same namespace. It's quite a headache.

  13. PieterjanDeClippel commented on Oct 15, 2020

    @PieterjanDeClippel

    In our case we have a project where we structure functionality under a seperate folder. Like everything for Customer is being filed under the Customer folder. But since there's also a DTO called Customer in another project we use the CustomerModule namespace for the classes in the Customer folder.

    Now each time you add a new class it's being added in the Customer namespace. It would be handy if you could have a folderconfig or maybe projectconfig file (one in the entire project) where you could specify the options for class files for a specific folder/project

  14. drewnoakes commented on Oct 15, 2020

    @drewnoakes
    Member

    In addition to specifying that a folder does not participate in namespace construction, I can see it being useful to also specify the full namespace for a given folder, such that it ignores any project-level namespace and ancestor folder names.

  15. 29 remaining items

  16. dustinlacewell commented on Oct 18, 2024

    @dustinlacewell

    This is a frustrating property of Visual Studio's namespace handling. Your options are basically:

    • Folders are only a direct representation of your namespace, period.
    • You can use folders for other purposes, but, get no help automating namespaces whatsoever.

    Is there a deal between JetBrains and Microsoft to keep this the status quo to drive sales to resharper? I'm kidding, but c'mon how do we live like this? :P

  17. CodeAngry commented on Oct 27, 2024

    @CodeAngry

    @dustinlacewell Because they can afford to (somehow) take years before they add a very basic nothing in the tooling. It's been literally 5 years since this was opened...

  18. drewnoakes commented on Jan 16, 2025

    @drewnoakes
    Member

    Potentially address #9626 at the same time.

  19. Matt-17 commented on May 3, 2025

    @Matt-17

    Are there any news on this and even considered to be implemented any time?

  20. sharpjs commented on Aug 1, 2025

    @sharpjs

    Recently, I was asked to complete a Visual Studio survey. One of the questions asked what improvement I wanted most. I linked to this issue.

  21. kjkrum commented on Aug 3, 2025

    @kjkrum

    Definitely in my top five. And that's hedging because I can only think of this and two other issues I care about... all ancient.

  22. sdecoodt commented on Oct 31, 2025

    @sdecoodt

    Any progress on this feature?
    I feel like this one just got stuck in analysis paralysis.

    The "beauty" of the resharper solution is that it only targets one problem, excluding one folder from the namespace.
    From some of the suggestions I've read here it sounds like completely overhauling namespace mechanics, with some sort of "shadow" structure that for each folder (node) determines the (0 or more) namespace segments.
    This structure would then have to feed in to the analysers and templating as input to make them behave. No to mention the tooling Visual studio and C# DevKit should be able to configure that structure somewhere.

    I can see how a simple "ignore folder" feature would then becoma a daunting task.
    This has allready changed from a feature request to a feature, but I wonder what the scope of this actual feature is at this point. Features with an unclear scope tend to get pushed backwards...

  23. EthanJamesBarron commented on Nov 21, 2025

    @EthanJamesBarron

    Bump, been looking for YEARS!!!!!

  24. kjkrum commented on Nov 21, 2025

    @kjkrum

    And you'll keep waiting. This is one of those "what the hell is going on at Microsoft?" issues.

  25. redwyre commented on Jan 9, 2026

    @redwyre

    I would really like to see this supported in .editorconfig for Unity, because we can't modify the project files. (Though I have no problem if it does or doesn't get implemented in the projects as well).

    For Unity devs, the solution would be putting a (second) .editorconfig file in Assets\Scripts with the content

    [*.cs]
    dotnet_root_namespace = "MyProject"
    

    This belongs in the .editorconfig because the editor tooling is what is generating the namespace in the files. It would also have to be respected for other refactorings like moving files and extracting types.

  26. drewnoakes commented on Jan 21, 2026

    @drewnoakes
    Member

    @redwyre I'm not familiar with how Unity lays out its projects, but if you cannot modify the project, can you at least add a Directory.Build.props file that lets you set <RootNamespace> for that folder?

  27. redwyre commented on Jan 21, 2026

    @redwyre

    @redwyre I'm not familiar with how Unity lays out its projects, but if you cannot modify the project, can you at least add a Directory.Build.props file that lets you set <RootNamespace> for that folder?

    I can put that file there, but it would be only for editor tooling, as Unity doesn't use msbuild (at least, at this point it time) to build projects. Either way I would still prefer the .editorconfig setup.

    Looking at it again, shouldn't it be possible to also put something like this in the root .editorconfig rather than creating a new one?

    [Assets\Scripts\**.cs]
    dotnet_root_namespace = "MyProject"
    
  28. kjkrum commented on Jan 21, 2026

    @kjkrum

    @redwyre The goal is to change the default namespace for a directory within a project.

    My use case is a shared-source application that is expected to be customized in code. Wherever possible, the customizations take the form of partial classes. This simplifies development by turning merge conflicts into compiler errors when the main branch is updated and the customization branch is rebased. (The whole thing is aimed at super low TCO.)

    I would like to establish a convention where customizations are placed in a directory called Customizations but the default namespace of C# files in that directory would be MyApp instead of MyApp.Customizations.

  29. sharpjs commented on Jan 26, 2026

    @sharpjs

    My use case also is to organize files into a subdirectory without having that specific subdirectory contribute to the default namespace.

    For example, if an app or library has many top-level source files, I would like to organize them into a Core subdirectory without the annoyance of removing .Core from the namespace in every new file. Another example is wanting to organize a very long list of files into A-E, F-L, etc. subfolders. I can give numerous other examples. Is it a critical need? No. It's just unnecessary friction and perhaps an easy opportunity to get a win for developer ergonomics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Triage-Approved

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions