Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

are namespace definitions unique?

Status
Not open for further replies.

jby1

Programmer
Apr 29, 2003
403
0
0
GB
Hi

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:

namespace service
namespace database.service

Can I do this?
 
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.)
 
Thank you for your response.

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.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top