zoukankan      html  css  js  c++  java
  • Initialize Static Class Members with Static Constructors


    You know that you should initialize static member variables in a type before you create any instances of that type. C# lets you use static initializers and a static constructor for this purpose. A static constructor is a special function that executes before any other methods, variables, or properties defined in that class are accessed. You use this function to initialize static variables, enforce the singleton pattern, or perform any other necessary work before a class is usable. You should not use your instance constructors, some special private function, or any other idiom to initialize static variables.

    As with instance initialization, you can use the initializer syntax as an alternative to the static constructor. If you simply need to allocate a static member, use the initializer syntax. When you have more complicated logic to initialize static member variables, create a static constructor.

    Implementing the singleton pattern in C# is the most frequent use of a static constructor. Make your instance constructor private, and add an initializer:

    public class MySingleton
    {
    private static readonly MySingleton theOneAndOnly = new MySingleton();
    public static MySingleton TheOnly
    {
    get
    {
    return theOneAndOnly;
    }
    }
    private MySingleton()
    {
    }
    // remainder elided
    }



    The singleton pattern can just as easily be written this way,in case you have more complicated logic to initialize the singleton:

    public class MySingleton
    {
    private static readonly MySingleton theOneAndOnly;
    static MySingleton()
    {
    TheOneAndOnly = new MySingleton();
    }

    public static MySingleton TheOnly
    {
    get
    {
    return _theOneAndOnly;
    }
    }
    private MySingleton()
    {
    }
    // remainder elided
    }



    As with instance initializers, the static initializers are called before any static constructors are called. And, yes, your static initializers execute before the base class's static constructor.



    The CLR calls your static constructor automatically when your type is first loaded into an application space. You can define only one static constructor, and it must not take any arguments. Because static constructors are called by the CLR, you must be careful about exceptions generated in them. If you let an exception escape a static constructor, the CLR will terminate your program. Exceptions are the most common reason to use the static constructor instead of static initializers. If you use static initializers, you cannot catch the exceptions yourself. With a static constructor, you can (see Item 45):

    static MySingleton()
    {
    try
    {
    theOneAndOnly = new MySingleton();
    }
    catch
    {
    // Attempt recovery here.
    }
    }



    Static initializers and static constructors provide the cleanest, clearest way to initialize static members of your class. They are easy to read and easy to get correct. They were added to the language to specifically address the difficulties involved with initializing static members in other languages.
  • 相关阅读:
    【Vue】 computed和watch的区别
    【总结】html5新增标签以及css3新增属性
    【前端经典面试题】前后端分离(说一说你理解的前后端分离?)
    【前端面试CSS】—垂直水平居中终极版
    【CSS】清浮动的常用四种方法
    【前端面试CSS】——盒子模型(标准模型和怪异模型)
    【JavaScript】JS常用的方法总结(不定期更新中)
    【Cordova】开发app遇到的坑之 按两次返回键退出程序
    【JavaScript】深拷贝和浅拷贝的总结(含义、区别及实现)
    【vue】路由传参的三种方式
  • 原文地址:https://www.cnblogs.com/simonhaninmelbourne/p/1559364.html
Copyright © 2011-2022 走看看