何时定义静态构造函数以及何时隐式调用它们? [英] When to define static constructors and when are they implicitly called???

查看:121
本文介绍了何时定义静态构造函数以及何时隐式调用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想知道什么时候应该定义静态构造函数,或者它们甚至是否需要
???另外,它们何时被隐含地称为???

Bob Rock

解决方案

如果需要初始化,则使用静态构造函数静态成员

变量。

如果指定BeforeFieldInit属性,CLR会在第一次访问静态变量时调用静态

构造函数。 br />

" Bob Rock" <无*************************** @ hotmail.com> ha scritto nel

messaggio新闻:O5 ************** @ tk2msftngp13.phx.gbl ...

你好,

我想知道什么时候应该定义静态构造函数,或者他们甚至需要
???另外,它们何时被隐含地称为???

Bob Rock



每当应该实现静态构造函数你有任何静态的

属性

在类中定义,以初始化这些属性。

在静态构造函数中进行初始化更简洁如果你问我,直接初始化声明的地方比



当你创建类的实例时,静态构造函数会运行

它被声明为

或者使用在类中声明的任何静态方法。

它仅在当前没有任何其他程序集实例的情况下运行

运行,

因为它已经运行了。


Bob Rock <无*************************** @ hotmail.com>在消息中写道

新闻:O5 ************** @ tk2msftngp13.phx.gbl ...

你好,
我想知道什么时候应该定义静态构造函数,或者他们甚至需要
???此外,他们何时被隐含地称为???

Bob Rock



Xian< x.ian @ _TOGLIERE_libero.it>写道:

如果需要初始化静态成员
变量,则使用静态构造函数。


好​​吧,我认为它有两种不同的用途:


1)你需要以更复杂的方式初始化静态变量比

正常


static int foo = ...;


轻松允许。


2)你想阻止将BeforeFieldInit标志添加到

类(见下文)

如果指定BeforeFieldInit属性, CLR在第一次访问静态变量时调用静态
构造函数。




不完全 - 它在之前的某个时间执行它第一次访问

静态变量。


注意,只要有
* isn'',C#会自动添加BeforeFieldInit t *一个静态构造函数。


参见 http://www.pobox.com/~skeet/csharp/beforefieldinit.html 了解更多

信息。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


Hello,

I was wondering when should static constructors be defined or are they even
required??? Also, when are they implicitly called???
Bob Rock

解决方案

static constructor is used if you need to initialize static member
variables.
If you specify the BeforeFieldInit attribute, CLR invokes the static
constructor at the first access of a static variable.

"Bob Rock" <no***************************@hotmail.com> ha scritto nel
messaggio news:O5**************@tk2msftngp13.phx.gbl...

Hello,

I was wondering when should static constructors be defined or are they even required??? Also, when are they implicitly called???
Bob Rock



A static constructor should be implemented whenever you have any static
properties
defined in the class, to initialize those properties.
It is cleaner to do the initialization in the static constructor than
to directly initialize where declared if you ask me.

The static constructor runs when you create an instance of the class where
it is declared
or use any static methods declared in the class.
It runs only if there is not any other instance of the assembly currently
running,
because then it has already run.

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...

Hello,

I was wondering when should static constructors be defined or are they even required??? Also, when are they implicitly called???
Bob Rock



Xian <x.ian@_TOGLIERE_libero.it> wrote:

static constructor is used if you need to initialize static member
variables.
Well, I see it as being useful in two different ways:

1) You need to initialize static variables in a more complex way than
the normal

static int foo = ...;

easily allows.

2) You want to prevent the BeforeFieldInit flag from being added to the
class (see below)
If you specify the BeforeFieldInit attribute, CLR invokes the static
constructor at the first access of a static variable.



Not quite - it does it "at some time before" the first access of a
static variable.

Note that BeforeFieldInit is added automatically by C# whenever there
*isn''t* a static constructor.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于何时定义静态构造函数以及何时隐式调用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆