0

I am creating a C# assembly which has logical folders in it. Every time I add a file into a folder it has a different namespace because of the folder.

I am curious that should I rename each one to same namespace or leave it as it is.

Which one is best practice?

DocMax
  • 12,094
  • 7
  • 44
  • 44
D J
  • 6,908
  • 13
  • 43
  • 75
  • I thinks that if each folder contains classes that belong to different logical section of the application it is considered a good practice to have a different namespace – Shai Aharoni Feb 06 '13 at 07:57
  • possible duplicate of [C# Namespaces and Assemblies Best Practice](http://stackoverflow.com/questions/8934570/c-sharp-namespaces-and-assemblies-best-practice) – Tim M. Feb 06 '13 at 07:59
  • the duplicate question does not give any concrete answer. – D J Feb 06 '13 at 08:05

2 Answers2

3

Namespace organize code elements and creates globally unique types. If you want your code to be in a single namespace then sure you can rename all of them, but since you have your code in different folder for organization you can leave them in different namespace for clarity. But it depends on you and your requirement.

Habib
  • 219,104
  • 29
  • 407
  • 436
3

You should definitely leave them as they are. If you feel that you need to create folders for classes within your assembly, because you feel that they are logically or semantically related, then the namespace should reflect that.

Here's a good argument for creating namespaces for each folder.

Community
  • 1
  • 1
levelnis
  • 7,665
  • 6
  • 37
  • 61
  • Agreed - either there is a good reason for using subfolders, and the same reason should apply to the namespace naming - or there isn't, in which case you shouldn't be using a subfolder. – Matthew Watson Feb 06 '13 at 08:49