Static Constructors
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的更多相关文章
- static, readonly, const
static Use the static modifier to declare a static member, which belongs to the type itself rather t ...
- Static Classes and Static Class Members
Static Classes and Static Class Members A static class is basically the same as a non-static class, ...
- c++ 初始化静态static成员变量或static复合成员变量
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c https://sta ...
- The Similarities and Differences Between C# and Java -- Part 1(译)
原文地址 目录 介绍(Introduction) 相似点(Similarities) 编译单位(Compiled Units) 命名空间(Namespaces) 顶层成员(类型)(Top Level ...
- Nhibernate 4.0 教程入门
Nhibernate 4.0 教程 目录 1. 下载Nhibernate 4.04. 1 2. 入门教程... 2 3. 测试项目详解... 3 4. 总结.. ...
- Android Bootloader LittleKernel的两篇文章 【转】
转自:http://blog.csdn.net/loongembedded/article/details/41747523 2014-12-05 14:37 3599人阅读 评论(2) 收藏 举报 ...
- CLR via C# 3rd - 08 - Methods
Kinds of methods Constructors Type constructors Overload operators Type con ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(六)SendReceiveOptions
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- C#复习④
C#复习④ 2016年6月16日 12:37 Main Classes and Structs 类和结构体 1.Contents of Classes 字段,常量,方法,构造函数,析构函数: 特性,事 ...
随机推荐
- Linux 本人常用到的基本命令
cat -n FileName //查看FileName文件的内容.-n显示对应行号. yum install SoftName //安装软件,切记使用root权限. service //查看服务.例 ...
- 有了第一台自己开发的pro,开心,明天分享最近整理逆向分析ios的一些东西
最近都在忙于ios深入研究,研究别人的代码,别人的app.然后顺藤摸瓜的找到了关键:逆向,动态特性等. 相关工具:reveal, cycript等. 特别感谢前人的分享,为了打开学习ios的另外一扇大 ...
- primefaces 知识点整理
database组件中设置数据中一行的颜色 需要设置属性: rowStyleClass="#{data.tradeMoney le 0 ? 'exp' : null}"exp样式: ...
- java 动态编译
public class Main { public static void main(String[] args) { System.out.println("Hello World!&q ...
- PowerDesigner15中定义varbinary(max)列
PowerDesigner15 概念数据模型(Entity)中要定义数据类型为varbinary(max)的特性(Attribute),应将数据类型(Data Type)选择为other,在代码(Co ...
- MongoDB-JAVA-Driver 3.2版本常用代码全整理(3) - 聚合
MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别.例如用Document替换BasicDBObject.通过Builders类构建Bson替代直接输入$命令等 ...
- .net该的帐
1.web api 2.socket通信 3.NUnit单元测试 4.了解自动化测试各种工具
- servletconfig和servletContext的区别
1.servletConfig: 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数.(配置在某个servlet标签或者整个we ...
- ORACLE rowid,file# 和 rfile#
rowid简介 rowid就是唯一标志记录物理位置的一个id,在oracle 8版本以前,rowid由file#+block#+row#组成,占用6个bytes的空间,10 bit 的 file# , ...
- ORACLE object_id和data_object_id
object_id和data_object_id 都是对象的唯一标识. object_id是对象的逻辑标识 data_object_id是对象的物理标识 对于没有物理存储的对象,data_object ...