简单介绍:

  1. 一个类只能有一个静态构造函数。
  2. 静态构造函数只能访问类的静态成员
  3. 静态构造函数不能传入参数
  4. 静态构造函数只会在该类第一次被使用的时候执行。

 

下面代码演示:

TestClass.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace StaticClassMethod
{
    public static class TestClass
    {
        //静态类构造函数
        static TestClass()
        {
            Console.WriteLine("Andrew's Blog..");
        }

        public static void PrintStr(string printStr)
        {
            Console.WriteLine(printStr);
        }
    }
}

 

Program.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace StaticClassMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass.PrintStr("打印字符串测试");
            TestClass.PrintStr("再次测试一次");
            Console.ReadLine();
        }
    }
}

 

运行结果:

C# 静态类的构造函数

 

以上例子下载:https://files.cnblogs.com/andrew-blog/StaticClassMethod.rar

参考:http://www.wxzzz.com/?id=103

 

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2021-11-20
  • 2021-06-16
  • 2021-08-23
  • 2021-11-22
  • 2021-11-15
猜你喜欢
  • 2022-12-23
  • 2021-08-27
  • 2021-11-02
  • 2021-12-18
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案