Not sure if this follows the rules or not, but I need some help with names. How should I approach project name vs namespace vs class. For example, I want to make a datacleaner program. So I name it Datacleaner, and then DC for the class, and then Cleaner.cs for the file name, and it just gets all confusing. Is there some best practice I can be following here!? A helpful mindset or naming theory would be exceptionally helpful.
-
5Project name & namespace name should generally be the same, as should file name & class name. – SLaks Dec 09 '13 at 18:04
-
possible duplicate of [Naming Convention in c#](http://stackoverflow.com/questions/1618316/naming-convention-in-c-sharp) – Davide Aversa Dec 09 '13 at 18:06
5 Answers
Microsoft has an excellent set of guidelines - See the next link: Guidelines for Names Also see the Naming section
I think you'll find there all the info you need.
This question already asked in StackOverflow, see here
It is slightly off-topic, but I'll give you a shot here.
The first thing we need to look at, is the root namespace. Depending on what you're doing, this root-namespace may be shared across multiple projects. A good example of this is System
. You might put your company name there, or you might choose something more eclectic. If I am writing library code, I avoid things like DataCleaner
because libraries are supposed to be generic and don't pertain to a common form.
So, you're writing a data cleaner. Great! There's nothing wrong with having a common library and having a Data
namespace (maybe you want to add more things pertaining to data in the future), and then you have your Cleaner
class.
If I were a user of your API, I'd happily understand that <library>.Data
was a namespace and I need to be looking for Cleaner
.

- 38,257
- 10
- 78
- 128
You should Mike Roberts' series on How to Set Up A .Net Development Tree. It's a bit dated, but the concepts still hold true. Links to his articles are in my answer here: https://stackoverflow.com/a/9982500/467473 (he seems to have rearranged his blog and broken the links therein, though the content is still there). Also see Tree Surgeon, a tool for creating solutions using the principles Mike Roberts espoused.
In a nutshell, lay out your source tree thusly:
Fundamentally, your directory structure should look like this:
Meta/Development Root
Usually mapped to root of source control system.Solution
One directory, contain your entire solution. Should be named to match the solution.Solution.sln
The solution file itself.nant.build
The nAnt build file for the solution.lib
Thelib
directory contains 3rd party assemblies/dlls that are referenced by the different projects in your solution. It is under source control. Project references should point here.tools
Thetools
directory contains all 3rd party tools required to build your solution. It, too, is under source control. Thetools
directory should contain the versions ofnAnt
,nUnit
etc. used by your project — and your build scripts should reference these, rather than the version(s) installed on a developer's machine.bin
Thebin
directory contains the output of the build process for the solution. Each projects must be configured to point here.debug
debug buildrelease
release build
obj
In the ideal world, each project'sobj
would be pointed here as well as this has no place in the source tree. Sadly, Visual Studio doesn't offer an official way to do that (though, I'm told, VS can be hacked to do so if you're resourceful enough).src
Thesrc
directory is the root directory for the actual source code of your solution.project1
The directory forproject1
.project
.csproj`
The project file.*.cs
, etc. files. The source files.
- ...
project-n
The src
directory contains the actual source code. Each project should be named with its full namespace. Whether you lay them out flat or build out the whole namespace structure in the file system is up to you. The idea is that the namespace should guide you to the source file.

- 1
- 1

- 71,308
- 16
- 93
- 135
Here is a short summary of conventions from another SO question:

- 1
- 1

- 3,939
- 2
- 26
- 37
The other answers here are all spot on, for C# you probably want to follow the naming conventions put forth by Microsoft (and are automatically used by default if you are developing with Visual Studio) unless you have a compelling reason not to.
Project Namespace should match the Project Name or should match the Project Name with some standardized prefix prepended.
Conventions for class and files are 1 class per file and the file name should match the class name.
You are forbidden to name give a class the same name as it's namespace, which I assume is the issue you ran into that prompted this question. The solution to that is probably to give the project a more general name.
To give a more concrete example of what we do where I work.
Any product we produce here we typically have a solution with two or more projects in it. The default namespace for any project follows: ...
If the DataCleaner product had a windows service and a command line tool that dealt with the same domain you might have three projects: Console, Service, and Domain with name spaces of
Company.Team.DataCleaner.Console
Company.Team.DataCleaner.Service
Company.Team.DataCleaner.Domain
For naming of classes (and by extension the files they reside in), if you follow the above scheme you already somewhat get around the problem of having a DataCleaner
class in your DataCleaner
namespace, but you may find application of the Single Responsibility Principle useful. To put it simply, any class should only do one thing. If you have a class named Cleaner
or DataCleaner
they might be trying to do too much, and breaking it up would result in names specific to the resulting classes

- 4,079
- 2
- 19
- 25