我认识的人刚刚让我解释MSDN上的这个声明,我傻眼了。
常量或类型声明隐含地是静态成员。
“或类型声明隐含地是静态成员”这句话对我来说没有意义。
这意味着什么?
对我来说,一个type declaration is implicitly a static member
.
因为如果你有课:
class Foo
{
public class Bar
{
}
}
您无法通过以下方式访问课程Bar
:
Foo f = new Foo();
Bar b =new f.Bar();
(我什至不确定如何编写它才能使其有意义)。
如果要访问Bar
类,则需要执行以下操作:
Bar b = new Foo.Bar()
您可以通过类而不是实例来访问它。
因此,是的Bar
静态成员Foo
。
在本文的上下文中,我相信他们将类型定义为简单的 inners struct
、inneresclass
和enum
s 的定义——它们总是可以在静态上下文中作为类型引用。
这意味着当你定义一个这样的类时,
public class Message
{
const int i = 10;
enum NewType{ typeval, typevale2 }
}
这里两者都是隐式静态成员。
Const 是隐式静态的,但它与普通静态字段的不同之处在于它在程序执行期间无法更改。虽然它仍然是静态的..
类型声明
类型声明是类声明(第 10.1 节)、结构声明(第 11.1 节)、接口声明(第 13.1 节)、枚举声明(第 14.1 节)或委托声明(第 15.1 节) .
来自类型声明MSDN Doc
静态成员
When a field, method, property, event, operator, or constructor declaration includes a static modifier, it declares a static member. In addition, a constant or type declaration implicitly declares a static member. Static members have the following characteristics:
- When a static member M is referenced in a member-access (Section 7.5.4) of the form E.M,E must denote a type containing M. It is a compile-time error for E to denote an instance.
- A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.
- A static function member (method, property, event, operator, or constructor) does not operate on a specific instance, and it is a compile-time error to refer to this in such a function member.
from Static and instance members MSDN Doc
So that means Constants
and all kind of Type declaration
are static without add keyword static
.