首页 > 代码库 > C#容易被忽视的知识点(三)

C#容易被忽视的知识点(三)

13、静态构造函数

  用途:用于初始化任何静态数据,或用于仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。

  特征:1、这种构造函数只执行一次,即在代码引用类之前调用它。

     2、静态构造函数没有访问修饰符,其他C#代码不能调用,在加载类时,由.NET运行库调用。

     3、出于同样的原因,静态构造函数不能带任何参数。

     4、一个类只能有一个静态构造函数。

     5、只能访问类的静态成员,不能访问类的实例成员。

     6、无参数的构造函数与静态构造函数可以在同一个类中同时定义,在加载类时执行静态构造函数,而在创建实例时执行实例构造函数。不会冲突。

  例子

 1    class StaticConstructorSample 2     { 3         public static readonly ConsoleColor backColor; 4  5         /// <summary> 6         /// 静态构造函数 7         /// </summary> 8         static StaticConstructorSample() 9         {10             DateTime now = DateTime.Now;11             if (now.DayOfWeek == DayOfWeek.Saturday12                 || now.DayOfWeek == DayOfWeek.Sunday)13             {14                 backColor = ConsoleColor.Blue;15             }16             else17                 backColor = ConsoleColor.Red;18         }19 20         /// <summary>21         /// 实例构造函数22         /// </summary>23         private StaticConstructorSample()24         {25         }26 27 28         /// <summary>29         /// 程序主入口30         /// </summary>31         public static void Main()32         {33             Console.ForegroundColor = backColor;34             Console.WriteLine("Hello");35             Console.Read();36         }37     }

 


14、构造函数初始化器

 1     class StaticConstructorSample 2     { 3         public static readonly ConsoleColor backColor; 4         private string id; 5         private string name; 6         private int age; 7  8         /// <summary> 9         /// 静态构造函数10         /// </summary>11         static StaticConstructorSample()12         {13             DateTime now = DateTime.Now;14             if (now.DayOfWeek == DayOfWeek.Saturday15                 || now.DayOfWeek == DayOfWeek.Sunday)16             {17                 backColor = ConsoleColor.Blue;18             }19             else20                 backColor = ConsoleColor.Red;21         }22 23         /// <summary>24         /// 实例构造函数25         /// </summary>26         private StaticConstructorSample()27         {28             this.id = Guid.NewGuid().ToString("N");29         }30 31         /// <summary>32         /// 构造函数初始化器33         /// 注意:会先执行没参数的构造函数,再执行有参数的构造函数,但不能有多个调用如34         ///       private StaticConstructorSample(string name) : this(),this(20)   是错误的35         /// </summary>36         /// <param name="name"></param>37         private StaticConstructorSample(string name)38             : this()39         {40             this.name = name;41         }42 43         /// <summary>44         /// 没有使用构造函数初始化器,不会先执行没有参数的构造函数45         /// </summary>46         /// <param name="age"></param>47         private StaticConstructorSample(int age)48         {49             this.age = age;50         }51 52         /// <summary>53         /// 程序主入口54         /// </summary>55         public static void Main()56         {57             Console.ForegroundColor = backColor;58 59             StaticConstructorSample sample1 = new StaticConstructorSample("Latin");60             Console.WriteLine("the id of sample is : {0}",sample1.id);61             Console.WriteLine("the name of sample is : {0}", sample1.name);62             Console.WriteLine("the age of sample is : {0}", sample1.age);63 64             Console.WriteLine();65 66             StaticConstructorSample sample2 = new StaticConstructorSample(10);67             Console.WriteLine("the id of sample is : {0}", sample2.id);68             Console.WriteLine("the name of sample is : {0}", sample2.name);69             Console.WriteLine("the age of sample is : {0}", sample2.age);70 71 72             Console.Read();73         }74     }

 

C#容易被忽视的知识点(三)