微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#中的静态构造函数有什么用途?

C#中的静态构造函数有什么用途?

静态构造函数用于初始化任何静态数据,或执行特定的操作 需要翻译的内容为:在首次调用之前自动执行的操作 实例被创建或任何静态成员被引用时。

当为非托管代码创建包装类时,静态构造函数非常有用, 当构造函数可以调用LoadLibrary方法时。静态构造函数也是如此 方便的地方来对无法进行运行时检查的类型参数进行强制执行 在编译时通过约束条件进行检查。

静态构造函数具有以下属性

公共语言运行时(CLR)。它会自动调用
  • 用户无法控制静态构造函数何时执行 程序。

  • 静态构造函数在初始化类之前自动调用一个实例被创建或引用任何静态成员。一个静态 构造函数将在实例构造函数之前运行。类型的静态构造函数 当静态方法分配给事件或委托并被调用时被称为 并且不是在被分配时。如果静态字段变量初始化器存在于中 class of the static constructor, they will be executed in the textual order in 静态构造函数的类,它们将按照文本顺序执行 在执行之前,它们出现在类声明中 如果您不提供静态构造函数来初始化静态字段,则所有静态字段将在首次访问时按照其声明顺序自动初始化。

  • 字段被初始化为它们在C#中认值中列出的值 如果静态构造函数抛出异常,运行时将不会调用它 第二次,而且类型将在其生命周期内保持未初始化状态 您的程序运行的应用程序域。最常见的情况是,一个 TypeInitializationException异常在静态构造函数被抛出时发生 无法实例化类型或在发生未处理的异常时 静态构造函数。对于未显式定义的隐式静态构造函数 在源代码中定义,故障排除可能需要检查 中间语言(IL)代码
  • 存在静态构造函数会阻止添加 BeforeFieldInit类型属性。这限制了运行时优化。

  • 一个声明为static readonly的字段只能作为其的一部分进行赋值 在声明或静态构造函数中。当没有显式的静态构造函数时 需要翻译的内容为:required, initialize static fields at declaration, rather than through a static 需要翻译的内容为:required, initialize static fields at declaration, rather than through a static 构造函数用于更好的运行时优化。

  • 示例

    实时演示

    using System;
    namespace DemoApplication{
       public class Program{
          static void Main(string[] args){
             Car user = new Car();
             Car user1 = new Car();
             Console.ReadLine();
          }
       }
       public class Car{
          static Car(){
             Console.WriteLine("Static constructor called");
          }
          public Car(){
             Console.WriteLine("Default constructor called");
          }
       }
    }

    输出

    Static constructor called
    Default constructor called
    Default constructor called

    在上面的例子中,我们可以看到静态构造函数只被调用一次。

    例子

    在线演示

    using System;
    using System.Threading;
    namespace DemoApplication{
       public class Car{
          protected static readonly DateTime globalStartTime;
          protected int RouteNumber { get; set; }
          static Car(){
             globalStartTime = DateTime.Now;
             Console.WriteLine($"Static constructor called. Global start time:
             {globalStartTime.ToLongTimeString()}");
          }
          public Car(int routeNum){
             RouteNumber = routeNum;
             Console.WriteLine($"Car {RouteNumber} is created.");
          }
          public void Drive(){
             TimeSpan elapsedtime = DateTime.Now - globalStartTime;
             Console.WriteLine($"Car {this.RouteNumber} is starting its route
             {elapsedtime.Milliseconds} minutes after global start time
             {globalStartTime.ToShortTimeString()}.");
          }
       }
       class TestCar{
          static void Main(){
             Car car1 = new Car(1);
             Car car2 = new Car(2);
             car1.Drive();
             Thread.Sleep(25);
             car2.Drive();
             Console.ReadLine();
          }
       }
    }

    输出

    Static constructor called. Global start time:
    7:09:06 AM
    Car 1 is created.
    Car 2 is created.
    Car 1 is starting its route25 minutes after global start time7:09 AM.
    Car 2 is starting its route50 minutes after global start time7:09 AM.

    以上就是C#中的静态构造函数有什么用途?的详细内容,更多请关注编程之家其它相关文章

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐