I'm moving files in my project and I cannot find clear answer - is namespace connected with location of the file? Do I need to change namespace when moving files?
Asked
Active
Viewed 2,239 times
1
-
You can try and see, this is the easiest way. – mybirthname Dec 06 '15 at 19:55
-
1Namespace is not connected with file location, therefore you do not need to change it when moving files. – Jure Dec 06 '15 at 20:16
-
1Although it isn't connected in anyway, some would suggest that you do change the namespace to match but that is personal preference. – James Croft Dec 06 '15 at 20:22
1 Answers
0
is namespace connected with location of the file?
Only by convention. It is not required, nor enforced by the compiler or runtime.
Note that in C#, you can nest namespaces, and can declare more than one namespace at any given level of nesting. Obviously only one declared namespace in a file can actually match the file's location. So it would not even be possible to require namespaces to match the file name, without abandoning those features in C#.
Do I need to change namespace when moving files?
No. See above.
You may be interested in related Stack Overflow Q&A's on the topic:
- How to make the C# namespace work like Java Packages so they rename automatically when moving them?
- How do I fix Visual Studio (2013) namespace collision when moving files
- Should the folders in a solution match the namespace?
The first two relate to management of files and namespaces when changing the location of source files, while the third is more about generally accepted conventions.

Peter Duniho
- 68,759
- 7
- 102
- 136