zoukankan      html  css  js  c++  java
  • C# 静态构造函数的使用

     class StaticClass
        {
            public static string Name { get; set; }
            static StaticClass()
            {
                Name = "Delphi";
            }
    
            public StaticClass()
            {
                Console.WriteLine("Default constructor");
            }
    
            public static void Test()
            {
                Console.WriteLine("hello,wrold");
            }
        }
    一个类只能有一个静态构造函数,该构造函数不能有访问修饰符,不能带任何参数,不能直接调用,
    无论创建了多少个类实例,其静态构造函数都只调用一次
    
    1.在类实例化的时候会首先调用静态构造函数,然后再调用默认构造函数;
    StaticClass sc = new StaticClass();
    
    2.调用类中的静态成员变量时会调用静态构造函数;
    var tmp = StaticClass.Name;
    
    3.调用类中的静态函数的时候也会调用静态构造函数;
    StaticClass.Test();
  • 相关阅读:
    [ext4]空间管理
    [ext4] 磁盘布局
    [ext4]磁盘布局
    [ext4]08 磁盘布局
    [ext4]07 磁盘布局
    [ext4]06 磁盘布局
    [ext4]05 磁盘布局
    jQuery之链式编程
    jQuery之排他思想
    jQuery之筛选方法
  • 原文地址:https://www.cnblogs.com/yeshuimaowei/p/6362339.html
Copyright © 2011-2022 走看看