[英]Does c# have something like 'child' namespaces, or package scoping like in Java?
I don't understand why an explicit reference isn't required in this situation: 我不明白为什么在这种情况下不需要显式引用:
//SomeStaticClass.cs
namespace WhyDontINeedUsingStatement {
public static class SomeStaticClass {
public static string Thingy {
get { return "Behold! A thingy!"; }
}
}
public class SomeNonStaticClass {
public void DoSomethingUseful() {
var foo = 9;
}
}
}
// /SomeNamespace/SomeBoringClass.cs
namespace WhyDontINeedUsingStatement.SomeNamespace {
public class SomeBoringClass {
public static void DoSomething() {
var whatever = SomeStaticClass.Thingy;
var blah = new SomeNonStaticClass();
blah.DoSomethingUseful();
}
}
}
Why doesn't this require a using WhyDontINeedUsingStatement
at the top? 为什么这不需要在顶部
using WhyDontINeedUsingStatement
? Aren't these separate namespaces, even though they start with the same thing? 即使它们以相同的东西开头,这些单独的名称空间不是吗?
I get that C# namespaces aren't quite the same thing as Java packages (and don't affect access control), but not why the second class is able to reference stuff from the first. 我知道C#名称空间与Java包不太一样(并且不影响访问控制),但是为什么第二个类却能够从第一个类中引用东西呢?
According to C# Language Specification Version 5.0, Section 9.2, it seems like using the .
根据C#语言规范版本5.0第9.2节,似乎使用
.
in a namespace declaration is syntactic sugar : 在名称空间声明中是语法糖:
The qualified-identifier of a namespace-declaration may be a single identifier or a sequence of identifiers separated by “.” tokens.
名称空间声明的合格标识符可以是单个标识符或由“。”标记分隔的标识符序列。 The latter form permits a program to define a nested namespace without lexically nesting several namespace declarations.
后一种形式允许程序定义嵌套的名称空间,而无需在词法上嵌套多个名称空间声明。 For example,
例如,
namespace N1.N2
{
class A {}
class B {}
}
is semantically equivalent to
在语义上等同于
namespace N1
{
namespace N2
{
class A {}
class B {}
}
}
So from inside of N2
you can see N1
, hence why you can use it. 因此,从
N2
内部您可以看到N1
,因此您可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.