我有几个问题涉及C#中的静态构造函数。
- 什么是静态构造函数,它们如何与非静态构造函数不同。
- 我们如何在应用程序中使用它们?
**已编辑
public class Test
{
// Static constructor:
static Test()
{
Console.WriteLine("Static constructor invoked.");
}
public static void TestMethod()
{
Console.WriteLine("TestMethod invoked.");
}
}
class Sample
{
static void Main()
{
Test.TestMethod();
}
}
输出: 静态构造函数被调用。 TestMethod被调用。 所以,这意味着静态构造函数只会被调用一次。如果我们再次调用Test.TestMethod(); 静态构造函数不会被调用。
任何指针或建议将不胜感激。
谢谢。