Skip to main content
 首页 » 编程设计

c#之使用静态构造函数时 Visual Studio 的智能感知中断

2024年02月06日12softidea

我对 visual studio 的智能感知有疑问。 每次我在结构中创建 C# 静态构造函数时,visual studio 的智能感知都会在尝试调用构造函数时中断。 它似乎找不到构造函数,甚至连默认构造函数都找不到。 有谁知道我为什么会遇到这个问题?

    public Triangle(int aX, int aY, int bX, int bY, int cX, int cY) 
    { 
        A = new Point(aX, bY); 
        B = new Point(bX, bY); 
        C = new Point(cX, cY); 
    } 
 
    public Triangle(Point a, Point b, Point c) 
    { 
        A = a; 
        B = b; 
        C = c; 
    } 
 
    public Triangle(Triangle value) 
    { 
        A = value.A; 
        B = value.B; 
        C = value.C; 
    } 
 
    static Triangle() 
    { 
        Empty = new Triangle(0, 0, 0, 0, 0, 0); 
    } 

请您参考如下方法:

你不能直接调用static constructor ,并在创建第一个实例或引用任何静态成员之前自动调用它来初始化类型。

这里是静态构造函数规范的总结

  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

关于 Visual Studio 中的问题:

我对其进行了测试,一切正常。