简单介绍:
- 一个类只能有一个静态构造函数。
- 静态构造函数只能访问类的静态成员
- 静态构造函数不能传入参数
- 静态构造函数只会在该类第一次被使用的时候执行。
下面代码演示:
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(); } } }
运行结果:
以上例子下载:http://files.cnblogs.com/andrew-blog/StaticClassMethod.rar
参考:http://www.wxzzz.com/?id=103