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. js动画性能提升笔记

    JavaScript动画的性能并不亚于CSS动画.因此,如果使用了现代的动画库,例如Velocity,那么动画引擎的性能将不再是app的瓶颈,构成瓶颈的只有代码. 网络性能相关 动画是浏览器运行中资源 ...

  2. maven安装(linux)

    1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载文件夹,找到下载的文件,运 ...

  3. C语言中的union

    1.union中可以定义多个成员,union的大小由最大的成员的大小决定. 2.union成员共享同一块大小的内存,一次只能使用其中的一个成员,与struct形成鲜明对比. 3.对某一个成员赋值,会覆 ...

  4. highcharts 统计的样式

    highcharts 官网:http://www.hcharts.cn/

  5. Func的介绍

    经常看到  Func<int, bool>...这样的写法,看到这样的就没有心思看下去了.我们学技术还是需要静下心来. 对Func<int,bool>的Func转到定义看它的解 ...

  6. MYSQL服务器字符集设置

    修改默认字符集 vi /etc/my.cnf 在[mysqld]下面加入default-character-set=utf8 在[client]下面加入default-character-set=ut ...

  7. linux下(Ubuntu、centos)添加永久静态路由的方法

    项目中经常遇到多网卡的服务器,但是一个服务器的默认网关只有一个,当需要在多个网络中访问的时候(特别是在公安.交警等政府项目中),就需要添加静态路由了. 添加静态路由的方法有很多种,下面介绍2种比较常见 ...

  8. Oracle 11g AWR和ADDM性能报告

    一.自动工作负载库(Automatic Workload Repository,AWR) 自动工作负载库(Automatic Workload Repository,AWR)是在Oracle公司提供的 ...

  9. 在VC项目中使用自定义资源

    这是我看到的比较实用性的技巧,分享给大家 一.前言 在VC环境中除了我们所常用的Dialog.Menu和Bitmap等标准资源类型之外,它还支持自定义资源类型(Custom Resource),我们自 ...

  10. redis与memcached比较

    引用自:http://blog.csdn.net/e_wsq/article/details/23551799 最近需要用no-sql数据库来保存大量的数据,插入和查询都比较频繁,相对而言查询更加频繁 ...