Home > Enterprise >  Referencing another namespace in using statement that shares same parent namespace
Referencing another namespace in using statement that shares same parent namespace

Time:07-31

Lets say we're in class A, which is in namespace Application.X, and we have another class B that's in namespace Application.Y.

If you want to reference something from class B in class A, which of the following using directive statements is correct (or if both correct, which is preferred):

using Application.Y;

or

using Y;

CodePudding user response:

Both are correct, as evidenced by the fact that the code compiles either way and the class found in both cases is the same.

As to which is preferred, the immediate answer is "whatever is specified in your coding standards". If I were writing the standards (or just programming for my own sake) I would choose the second because firstly it's shorter to read for the same information, and secondly the extra "Application" in the first option could lead to mild confusion. After all, if you're working in the Application namespace, why did your former self (or a colleague) take the time to specify?

That said, there are people who prefer prefixing uses of class variables with "this" on the theory that it's clearer, and I can easily see some subset of those preferring the first option as it's more explicit.

A final note is that code style tools are there to help you, not the other way around. If you don't like some of the tool's preferences, especially if you actually have a valid argument rather than "that's not how I learned", change them.

  • Related