静态构造函数的用法
静态构造函数用来初始化类中的一些静态字段或属性,在C#中第一次调用类的任何成员之前
执行静态构造函数。
public class UserPreferences { public static readonly Color BackColor; static UserPreferences() { DateTime now = DateTime.Now; if(now.DayOfWeek==DayOfWeek.Sunday) { BackColor=Color.Green; } else { BackColor=Color.Red; } } private UserPreferences { } }
测试静态构造函数:
class MainEntryPoint { static void Main(string[] args) { Console.WriteLine(UserPreferences.BackColor.ToString()); } }