2

If you add a directory in your Visual Studio project and you add a class inside it, the namespace will respect the whole path the directory inclusive.

But sometimes, I prefer having the class in the main project namespace, although it lies in a directory structure, just because I don't want to have mess in my code. So often happens that I rewrite the Myproject.MyDirectory namespace to be Myproject only.

Is it OK in your opinion? Or does any convention say that every class inside the directory must have it included in the namespace ?

Thanks

3 Answers 3

2

In my view, folders and namespaces serve for different purposes.

Folders are useful to provide a clear hierarchy for people who read your code

Namespaces are useful to provide a clear hierarchy for people who use your code. E.g. calling the API provided by your code, when they don't see the actual source code.

Therefore, inconsistency is perfectly fine, as long as each makes sense.

Sign up to request clarification or add additional context in comments.

Comments

2

There isn't any convention restricting from what you're trying to do. I usually have multiple directories in my project to organize class files and use different namespaces with no respect to the directory structure and I don't have any problems.

Comments

1

There is no constraint, but some think it's useful to have the namespace identical to the path. So you could choose any namespace and place the class file wherever you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.