Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Say I have the following structure in my solution:

MySolution -> ProjectA -> ClassA.cs

By defualt, the name of the first namespace in project ProjectA is ProjectA.

namespace ProjectA
{
    public class ClassA
    {
        // body
    }
}


Is it best/common practice for the name of the first namespace, within a project, to have the same name as the project in which it is found in?

Or should I change the defualt name of the first namespace?

What I have tried:

An answer from an experienced C# developer in Visual Studio is sought here.
Posted
Updated 5-Oct-21 20:18pm

It's good practice - it makes it a lot easier to work out where the namespace is when you look at a complex project with both many projects, and / or many references.
 
Share this answer
 
Comments
CBennigton 6-Oct-21 3:02am    
Are you saying "it's good practice" for the name of the first namespace, within a project, "to have" the same name as the project in which it is found in?

Or that "it's good practice" for the name of the first namespace, within a project, "to NOT have" the same name as the project in which it is found in?

Sorry, seeing as how I included two questions in my post I wanted to know which one you were agreeing with.
CHill60 6-Oct-21 3:47am    
I imagine OG was replying to the question in your header - i.e. it is good practice
Maybe you should read this concerning namespaces:
Namespaces | 2,000 Things You Should Know About C#[^]

and this:
The 9 Coding Standards C# Developers Need to Get Started[^]

Especially "7. Code File Organization" might be interesting for you.

In order to get a feeling about how to use namespaces please have a look at some GitHub projects e.g. https://github.com/JamesNK/Newtonsoft.Json[^]
 
Share this answer
 
v2
Comments
CBennigton 6-Oct-21 3:01am    
The first link was alright but the second link was much more explicit with answering my question. thanks.
TheRealSteveJudge 6-Oct-21 5:40am    
You're welcome! But why did you not accept the answer?
CBennigton 6-Oct-21 16:46pm    
For sure but after looking into "Namespaces | 2,000 Things You Should Know About" it gave the following statement ---> The highest two levels of namespaces should follow the pattern CompanyName.ProductName or CompanyName.TechnologyName. E.g. Acme.SearchEngine.Logging or Acme.Rendering.3DTools

Is that saying that my project should be called "CompanyName" (thus, the top level namespace being called CompanyName) while the nested namespace should be called (in this case) "ProductName" (e.g. CompanyName.ProductName) or "TechnologyName" (CompanyName.TechnologyName)?

Of course CompanyName, ProductName and TechnologyName are placeholders for a real company name, product name or technology name.

Hope to hear from you soon thanks.
CBennigton 7-Oct-21 0:13am    
Any thoughts Steve?
TheRealSteveJudge 7-Oct-21 2:32am    
Please see updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900