Namespace and Sub Namespaces
Asked Answered
W

6

22

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

Westfall answered 20/4, 2009 at 15:59 Comment(1)
What are you actually trying to achieve?Depicture
C
30

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.

Catoptrics answered 20/4, 2009 at 16:4 Comment(2)
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?Westfall
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.Catoptrics
E
9

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.

Exhalant answered 20/4, 2009 at 16:7 Comment(0)
B
1

No, not really. =)

Beaird answered 20/4, 2009 at 16:1 Comment(0)
F
1

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

Fumarole answered 20/4, 2009 at 16:2 Comment(0)
I
1

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;

Inaudible answered 20/4, 2009 at 17:15 Comment(0)
S
0

If I understand you correctly, your hierarchy looks like this

namespace Root
{
  namespace Root.Account
  {
     public class A{}
  }

  namespace Root.Orders
  {
    public class B{}
  }
}

What you can do is

using Root = Root;

and then access the classes in the sub namespaces like

var myClassA = Root.Account.A();

or

var myClassB = Root.Orders.B();

For further info, you can read the Microsoft docs about namespaces.

Svetlanasvoboda answered 19/3, 2021 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.