I am wondering if you can have more than one use of the same namespace definition. For example, I would like to have the following namespace definitions:
Yes, you can. Note that namespaces are all or nothing:
1) You can't start in the middle when using a namespace, for instance you can't say "Collections.ArrayList" in an application that has declared "using System;".
2) Sub namespaces under a namespace are not included for "free". For instance, you can't just write "ArrayList", relying on Collections to be there because it is under "System", which is named in the "using" section.
Because of this treatment of namespaces as just text strings, with or without the ".", "service" and "database.service" truly are unique namespaces. To show just how flat this is, this
namespace database {
namespace service {
some classes
}
}
Is actually little different from this
namespace database.service {
some classes
}
As a matter of fact, both forms could be mixed in the same program and treated as if they are the same.
The compiler/CLR cares about namespace "nesting" much less then programmers do.
The compiler/CLR don't mind you doing what you propose. Only other programmers might care, because it could lead to confusion for them. if "service" and "database.service" are unrelated, then so be it, but if "database.service" handles the database part of your service, maybe what you really mean to do is have "service" and "service.database", which is less confusing. While you are at it, you might think about naming "service" something more specific to your situation (unless you are writing classes to support generic services). You could try something like "LaundryService" and "LaundryService.Database". (Note that the FCL uses Pascal casing for namespaces.)
The problem I am having, which I kinda hinted at in another post to this forum today, is that I am try to access my service namespace under my database class (which contains the sub-namespace service). However, when I type service it assumes that I mean the database.service namespace!
I know I can use totally unique names for each namespace 'bit', was just hoping that I wouldn't have to.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.