静态构造函数的目的是什么 [英] what is the purpose of static constructors

查看:68
本文介绍了静态构造函数的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生。我想知道为什么使用静态构造函数和这个构造函数的目的是什么

解决方案

静态构造函数用于初始化任何静态数据,或执行仅需要执行一次的特定操作。

在创建第一个实例或引用任何静态成员之前自动调用它。





你可以从这里找到更多。

http:/ /msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx [ ^ ]



http://www.dotnetperls.com/static-constructor [ ^ ]


关于C#语言规范和C#程序员参考的静态构造函数的一些要点:



1)类的静态构造函数在创建类的任何实例之前执行。

2)类的静态构造函数在任何o之前执行f引用类的静态成员。

3)类的静态构造函数在类的静态字段初始值设定项(如果有)之后执行。

4)在单个程序实例化期间,类的静态构造函数最多执行一次

5)静态构造函数不接受访问修饰符或具有参数。

6)静态在创建第一个实例或引用任何静态成员之前,会自动调用构造函数来初始化类。

7)无法直接调用静态构造函数。

8)用户无法控制程序中何时执行静态构造函数。

9)静态构造函数的典型用法是当类使用日志文件并使用构造函数将条目写入此文件时。



参考链接: - C#中的静态构造函数及其用法。 [ ^ ]



并尝试进一步参考

http://msdn.microsoft.com/en-us/library/aa645612(v = vs.71).aspx [ ^ ]

http://www.dotnetperls.com/static-constructor [ ^ ]


什么是: 静态构造函数通常用于初始化应用于类而不是实例的
。因此,它用于在创建类的任何对象之前初始化类的各个方面。



需要注意的要点:



1。自动调用静态构造函数(当类第一次时加载并且在实例构造函数之前。



2。静态构造函数不能有访问修饰符(因此,他们使用默认访问权限)并且不能被您的程序调用。



用法:



1。静态构造函数的典型用法是当类使用日志文件时构造函数用于将条目写入此文件。



2。假设我们有一个使用数据库访问的静态类然后静态构造函数是初始化该数据库连接的便利位置,因为这意味着连接将在首次需要之前创建。



3。你好吗现在每个对象占用内存中的一些空间。但是通过使用静态构造函数,我们可以减少它。这意味着性能提升。



希望它有所帮助:)



进一步查询请注释!

hello sir., I want to know about why use static constructor and what is the purpose of this constructor

解决方案

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only.
It is called automatically before the first instance is created or any static members are referenced.


You can find more from here.
http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx[^]

http://www.dotnetperls.com/static-constructor[^]


Some important point regarding static constructor from C# Language Specification and C# Programmer's Reference :

1) The static constructor for a class executes before any instance of the class is created.
2) The static constructor for a class executes before any of the static members for the class are referenced.
3) The static constructor for a class executes after the static field initializers (if any) for the class.
4) The static constructor for a class executes at most one time during a single program instantiation
5) A static constructor does not take access modifiers or have parameters.
6) A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
7) A static constructor cannot be called directly.
8) The user has no control on when the static constructor is executed in the program.
9) A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Reference Link :- Static Constructor in C# and their Usages.[^]

and try also for further reference
http://msdn.microsoft.com/en-us/library/aa645612(v=vs.71).aspx[^]
http://www.dotnetperls.com/static-constructor[^]


What is: A static constructor is typically used to
initialize features that apply to a class rather than an instance. Thus, it is used to initialize aspects of a class before any objects of the class are created.

Points to be noted:

1.The static constructor is called automatically (when the class is first loaded) and before the instance constructor.

2.The static constructors cannot have access modifiers (thus, they use default access) and cannot be called by your program.

Usage:

1.A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

2.Suppose we have a static class that uses database access then a static constructor is a handy place to initialize that database connection, because it means that the connection will get created right before it is first needed.

3.As you know each object occupies some space in memory. But by using static constructor we can reduce it. That means increase in performance.

hope it helps :)

for further queries comment here!


这篇关于静态构造函数的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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