A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only.

It is called automatically before the first instance is created or any static members are referenced.

class SimpleClass
    // Static variable that must be initialized at run time.
    static readonly long baseline;

    // Static constructor is called at most one time, before any
    // instance constructor is invoked or member is accessed.
    static SimpleClass()
        baseline = DateTime.Now.Ticks;


Static constructors have the following properties:

  • 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.



In this example, class Bus has a static constructor.

When the first instance of Bus is created (bus1), the static constructor is invoked to initialize the class.

The sample output verifies that the static constructor runs only one time,

even though two instances of Bus are created, and that it runs before the instance constructor runs.

public class Bus
     // Static variable used by all Bus instances.
     // Represents the time the first bus of the day starts its route.
     protected static readonly DateTime globalStartTime;

     // Property for the number of each bus.
     protected int RouteNumber { get; set; }

     // Static constructor to initialize the static variable.
     // It is invoked before the first instance constructor is run.
     static Bus()
         globalStartTime = DateTime.Now;

         // The following statement produces the first line of output, 
         // and the line occurs only once.
         Console.WriteLine("Static constructor sets global start time to {0}",

     // Instance constructor.
     public Bus(int routeNum)
         RouteNumber = routeNum;
         Console.WriteLine("Bus #{0} is created.", RouteNumber);

     // Instance method.
     public void Drive()
         TimeSpan elapsedTime = DateTime.Now - globalStartTime;

         // For demonstration purposes we treat milliseconds as minutes to simulate
         // actual bus times. Do not do this in your actual bus schedule program!
         Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",

 class TestBus
     static void Main()
         // The creation of this instance activates the static constructor.
         Bus bus1 = new Bus(71);

         // Create a second bus.
         Bus bus2 = new Bus(72);

         // Send bus1 on its way.

         // Wait for bus2 to warm up.

         // Send bus2 on its way.

         // Keep the console window open in debug mode.
         System.Console.WriteLine("Press any key to exit.");
 /* Sample output:
     Static constructor sets global start time to 3:57:08 PM.
     Bus #71 is created.
     Bus #72 is created.
     71 is starting its route 6.00 minutes after global start time 3:57 PM.
     72 is starting its route 31.00 minutes after global start time 3:57 PM.      








专门建一个static public方法来初始化?这样用起来非常不方便,你需要在“第一次”使用静态成员前先调用这个方法。







using System.Net;
using System.Net.Sockets;
using System.Reflection;

namespace SuperSocket.ClientEngine
    public static partial class ConnectAsyncExtension
        private static readonly MethodInfo m_ConnectMethod;

        private static bool m_OSSupportsIPv4;

        static ConnectAsyncExtension()
            //.NET 4.0 has this method but Mono doesn't have
            m_ConnectMethod = typeof(Socket).GetMethod("ConnectAsync", BindingFlags.Public | BindingFlags.Static);

            //Socket.OSSupportsIPv4 doesn't exist in Mono
            var pro_OSSupportsIPv4 = typeof(Socket).GetProperty("OSSupportsIPv4", BindingFlags.Public | BindingFlags.Static);

            if (pro_OSSupportsIPv4 != null)
                m_OSSupportsIPv4 = (bool)pro_OSSupportsIPv4.GetValue(null, new object[0]);
                m_OSSupportsIPv4 = true;

        public static void ConnectAsync(this EndPoint remoteEndPoint, ConnectedCallback callback, object state)
            //Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, e);
            //Don't use Socket.ConnectAsync directly because Mono hasn't implement this method
            if (m_ConnectMethod != null)
                    new object[]
                        CreateSocketAsyncEventArgs(remoteEndPoint, callback, state)
                ConnectAsyncInternal(remoteEndPoint, callback, state);

        static partial void CreateAttempSocket(DnsConnectState connectState)
            if (Socket.OSSupportsIPv6)
                connectState.Socket6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

            if (m_OSSupportsIPv4)
                connectState.Socket4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


posted @ 2014-11-10 14:33  ChuckLu  阅读(660)  评论(0编辑  收藏  举报