运行库在何时初始化静态字段?
当定义静态字段时,情况变得更加复杂。
访问静态字段与此类似。
在这一阶段末尾,静态字段被初始化成默认值。
At the end of this phase, static fields are initialized to their default values.
第二个静态字段包括了PersonHome接口类。
The second static field holds the class of the PersonHome interface.
与使用任何其他静态字段一样使用该字段。
比如一个方法需要读取某个尚未加载的类的静态字段值。
An example might be a method that reads the value of a static field for a class that hasn't yet been loaded.
不支持同步静态字段。
FxCop建议在声明静态字段的位置对其进行初始化。
The recommendation from FxCop is to initialize static fields where they are declared.
也可以调用类的静态方法或访问类的静态字段。
You can also call static methods on classes and access classes' static fields. Observe!
静态字段不能被命名为m_XXX(自定义的命名规范)。
Static fields should not be named m_XXX (Custom naming conventions).
如果异常是从静态字段初始值设定项引发的,也存在同样的规则。
The same rules hold true if the exception is thrown from a static field initializer.
不是常数也不是只读字段的静态字段不是线程安全的。
Static fields that are neither constants nor read-only are not thread-safe.
第二个静态字段包括了PersonLocalHome接口的类。
The second static field holds the Class of the PersonLocalHome interface.
类或结构可以拥有实例字段或静态字段,或同时拥有两者。
A class or struct may have instance fields or static fields or both.
前接“statics”的名称表示在给定类静态字段根下的数据结构。
Names proceeded by "statics" indicate data structures rooted under a static field of the given class.
因此,您不应依赖于类构造函数来初始化线程相关的静态字段。
Therefore, you should not rely on class constructors to initialize thread-relative static fields.
直到第一次执行时才解析引用,这意味着没有地址可供从中加载该静态字段。
Until the first execution, the reference is unresolved, which means there's no address to load that static field from.
此程序还演示了静态字段初始值设定项如何在显式类型构造函数中的代码之前执行。
This program also demonstrates how static field initializers execute before the code inside an explicit type constructor.
实例化后,它会将当前的Transaction存储到线程静态字段中。
When instantiated, it could store the current Transaction into a thread-static field.
线程静态字段具有一个很好的属性,即一旦托管线程消失,就不会再将它们视为gc根。
A nice property of thread-static fields is that once the managed thread goes away, they are no longer treated as GC roots.
正如您在前面看到的那样,静态字段初始值设定项的代码在隐式类型构造函数的内部执行。
As you saw earlier, the code for a static field initializer executes inside an implicit type constructor.
当然,后续执行将从修补代码中获益,从而可以更直接地引用实例、静态字段或方法目标。
Subsequent executions, of course, will benefit from the result of patching the code so that the instance or static field, or method target, is more directly referenced.
由于这一决定使所有的Leaf相同,我们将用一个静态字段为Leaf保留一个单元素。
Because this decision makes all Leaves identical, we'll keep a singleton for Leaf in a static field.
在内部,Scope依赖线程静态字段存储提供给Scope构造函数的实例的Stack。
Internally, Scope relies on a thread-static field to store the Stack of instances supplied to Scope's constructor.
象对静态方法和类所做的那样,只要防止程序员在静态字段中引用泛型类型,情况就会好很多。
It would be much better to simply prevent the programmer from referring to generic types in static fields, as is done in the case of static methods and classes.
浅表副本创建与原始对象具有相同类型的新实例,然后复制原始对象的非静态字段。
A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic fields of the original object.
幸运的是,JSR - 14的最新版本(1.3)宣布在静态字段中使用类型参数是不合法的。
Luckily, the latest version of JSR-14 (1.3) outlaws the use of type parameters in static fields.
静态字段并不理想,但是当常数值不可能更改(例如一星期内的天数)时,也可以使用这种方法。
Static fields aren't ideal but are reasonably painless when the value of the constant isn't likely to change — such as the values for the days of the week.
第二种情况是,类型具有一个用于静态字段的初始值设定项,在这种情况下,编译器会在后台添加类型构造函数。
The second scenario is when a type has a initializer for a static field, in which case the compiler adds a type constructor behind the scenes.
应用推荐