[英]Stacking namespaces can't be referenced just like any other element?
So here is a snippet I made where one namespace in inside another (B is inside A). 所以这是我制作的一个片段,其中一个命名空间位于另一个内部(B位于A内部)。 Usually when you use 'using SpaceA;'
通常当您使用“使用SpaceA;”时 you can access all elements without typing SpaceA.
您无需输入SpaceA就可以访问所有元素。 This is the case for class A, but I cannot see SpaceB without having to type SpaceA.SpaceB.
类A就是这种情况,但是我必须输入SpaceA.SpaceB才能看到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;'
通常当您使用“使用SpaceA;”时 you can access all elements without typing SpaceA.
您无需输入SpaceA就可以访问所有元素。
Only the direct types which are members of SpaceA
. 仅属于
SpaceA
成员的直接类型。 A namespace-or-type-name
is never resolved using a namespace in a normal using
directive and then another "subnamespace" in the name. namespace-or-type-name
永远不会在常规using
指令中使用名称空间,然后在名称中using
另一个“子名称空间”进行解析。 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: 请注意,这与类型的声明方式无关(就具有嵌套的名称空间声明还是仅具有名称
namespace SpaceA.SpaceB
)-另一个示例将是:
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. 有关如何解析名称的确切详细信息,请参见C#5规范的3.8节。
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别名指令, 则可以在该位置使用它来查找其他名称空间
using X = System.Xml;
...
X.Linq.XElement x = null; // Valid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.