tags:

views:

675

answers:

5

Is there a way to use a namespace and then have it automatically use all sub namespaces?

Example:

namespace Root.Account
{
//code goes here
}

namespace Root.Orders
{
//code goes here
}


//New File:
using Root;

In order for me to use the code in Root.Account, I would need to add using Root.Account to my code.

I would like to be able to just say using Root and have it pick up any sub namespace classes for use.

If this makes sense, is this possible?

Thanks

+1  A: 

No, not really. =)

J. Steen
Well, thought I would try :) thanks!
Jason Heine
A: 

.NET/C# does not support this form of namespace using.

Daniel Brückner
+6  A: 

No, there's nothing working that way down the tree. However, you don't need to include using directives to go up the tree. In other words, if you use:

namespace Root.Orders
{
    // Code
}

then any types in Root are already visible.

Jon Skeet
Okay, this is interesting. So let's say I had a namespace Root.Orders.OrderItems and I used this in my directives, I would be able to utilize any class in Root and Root.Orders?
Jason Heine
No - it's only namespaces that are further up the hierarchy of the namespace of your type that are searched. In other words, it's the namespace directive that has this "feature", not the using directive.
Jon Skeet
+3  A: 

No, using directive in C# does not allow this:

Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

VB.NET, however, supports somewhat closer behavior with Imports statement:

The scope of the elements made available by an Imports statement depends on how specific you are when using the Imports statement. For example, if only a namespace is specified, all uniquely named members of that namespace, and members of modules within that namespace, are available without qualification. If both a namespace and the name of an element of that namespace are specified, only the members of that element are available without qualification.

That is, if you say

Imports Root

then you'll be able to just say Orders.Whatever.

Anton Gogolev
+1  A: 

From the way you stated the original question, it sounds like you may or may not realize that from just using:

using Root;

The way you clarify your code later on is:

Account.Class obj = new Account.Class();
Orders.Class obj = new Orders.Class();

You don't HAVE to have using Root.Account to access the code, and the same with Root.Orders. Other than that, no way to auto-recursively use namespaces. I think this would lead to abuse with using System;

Will Eddins