So here is a snippet I made where one namespace in inside another (B is inside A). Usually when you use 'using SpaceA;' you can access all elements without typing SpaceA. This is the case for class A, but I cannot see SpaceB without having to type SpaceA.SpaceB. Why is this? Here is the code:
using System;
using SpaceA;
namespace SpaceA
{
class A
{
public A()
{
Console.WriteLine("A");
}
}
namespace SpaceB
{
public class B
{
public B()
{
Console.WriteLine("B");
}
}
}
}
namespace TestingCSharp
{
class Program
{
static void Main(string[] args)
{
//This does not work
SpaceB.B x = new SpaceB.B();
//This does work
SpaceA.SpaceB.B y = new SpaceA.SpaceB.B();
}
}
}
Usually when you use 'using SpaceA;' you can access all elements without typing SpaceA.
Only the direct types which are members of SpaceA
. A namespace-or-type-name
is never resolved using a namespace in a normal using
directive and then another "subnamespace" in the name. Note that this has nothing to do with how the types are declared (in terms of having a nested namespace declaration or just namespace SpaceA.SpaceB
) - another example would be:
using System;
...
Xml.Linq.XElement x = null; // Invalid
See section 3.8 of the C# 5 specification for the precise details of how names are resolved.
One slight difference to this is if you have a using alias directive for a namespace, at which point that can be used to look up other namespaces
using X = System.Xml;
...
X.Linq.XElement x = null; // Valid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.