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

Static constructors have the following properties:

  • A static constructor does not take access modifiers or have parameters.

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members
    are referenced.

  • A static constructor cannot be called directly.

  • The user has no control on when the static constructor is executed in the program.

  • 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.

  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Example:

   class Student
{
public Student()
{
Console.WriteLine("Instance constructor");
} static Student()
{
Console.WriteLine("Static constructor");
}
}
Student stu = new Student();

output:

Static constructor

Instance constructor

Static Constructors的更多相关文章

  1. static, readonly, const

    static Use the static modifier to declare a static member, which belongs to the type itself rather t ...

  2. Static Classes and Static Class Members

    Static Classes and Static Class Members A static class is basically the same as a non-static class, ...

  3. c++ 初始化静态static成员变量或static复合成员变量

    https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c https://sta ...

  4. The Similarities and Differences Between C# and Java -- Part 1(译)

    原文地址 目录 介绍(Introduction) 相似点(Similarities) 编译单位(Compiled Units) 命名空间(Namespaces) 顶层成员(类型)(Top Level ...

  5. Nhibernate 4.0 教程入门

    Nhibernate 4.0 教程 目录 1.      下载Nhibernate 4.04. 1 2.      入门教程... 2 3.      测试项目详解... 3 4.      总结.. ...

  6. Android Bootloader LittleKernel的两篇文章 【转】

    转自:http://blog.csdn.net/loongembedded/article/details/41747523 2014-12-05 14:37 3599人阅读 评论(2) 收藏 举报 ...

  7. CLR via C# 3rd - 08 - Methods

       Kinds of methods        Constructors      Type constructors      Overload operators      Type con ...

  8. 介绍开源的.net通信框架NetworkComms框架 源码分析(六)SendReceiveOptions

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  9. C#复习④

    C#复习④ 2016年6月16日 12:37 Main Classes and Structs 类和结构体 1.Contents of Classes 字段,常量,方法,构造函数,析构函数: 特性,事 ...

随机推荐

  1. loadrunner11遇到的问题汇总及相应的解决方案(持续更新)

    1.在此界面点击Run Load Tests提示: "Can not save the license information because access to the registry  ...

  2. zedboard如何从PL端控制DDR读写(四)

    PS-PL之间的AXI 接口分为三种:• 通用 AXI(General Purpose AXI) — 一条 32 位数据总线,适合 PL 和 PS 之间的中低速通信.接口是透传的不带缓冲.总共有四个通 ...

  3. 通知 Notification

    1. NotificationManager ma=(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVIC ...

  4. 使用history.back()出现"警告: 网页已过期的解决办法"

    原因: 表单提交页面中使用了 session_start 函数.由于我们后退浏览的是缓存页,而该函数会强制当前页面不被缓存. 解决: PHP: 此提示出现在一个POST提交的页面,点到其它页面后,通过 ...

  5. 内置对象(Session、Application、ViewState)

    内置对象:为了跨页面传值和状态保持.→HTTP的无状态性 [4.]Session:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cook ...

  6. {CSDN}{英雄会}{砍树、石子游戏}

    砍树 思路: 可以将题目意图转化为:给定一棵树,求其中最接近总权值一半的子树. DFS求每个节点的所有子节点的权值和,遍历每个节点,最接近总权值一半的即为答案.复杂度O(N). 石子游戏: 思路: 一 ...

  7. memcpy code

    memcpy #include <stddef.h> //#include <stdint.h> //uintptr_t is quoted.#include "st ...

  8. VS&SQL StartUp Crash - CLR20R3

    VS2013和SQL Management Studio 在启动时直接崩溃,错误提示CLR20R3,问题签名4是windowsbase, 这说明是操作系统的问题导致启动崩溃,在网上找到一些解决方案: ...

  9. VC++ MFC子对话框建立与关闭

    主窗体 void CMoshiwindowDlg::OnButton1() { // TODO: Add your control notification handler code here CDi ...

  10. HTML 透明、阴影,圆角等知识点

     table两个属性:cellpadding:内容与单元格边框的距离,内部距离cellspacing:单元格之间的距离,外部距离    table合并边框线:  border-collapse: co ...