• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中的静态构造函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

https://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.140).aspx

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.

 

Example

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}",
             globalStartTime.ToLongTimeString());
     }

     // 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}.",
                                 this.RouteNumber,
                                 elapsedTime.TotalMilliseconds,
                                 globalStartTime.ToShortTimeString());
     }
 }

 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.
         bus1.Drive();

         // Wait for bus2 to warm up.
         System.Threading.Thread.Sleep(25);

         // Send bus2 on its way.
         bus2.Drive();

         // Keep the console window open in debug mode.
         System.Console.WriteLine("Press any key to exit.");
         System.Console.ReadKey();
     }
 }
 /* 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方法来初始化?这样用起来非常不方便,你需要在“第一次”使用静态成员前先调用这个方法。
如果你在使用静态成员前忘了调用该方法,会导致错误。
如果重复调用,又是冗繁操作。

所以静态构造函数就派上用场了。
它会在你第一次调用静态成员(或创建实例)的时候自动被调用

以上解释引自:http://zhidao.baidu.com/question/112464220.html

 

实例:

https://github.com/kerryjiang/SuperSocket.ClientEngine/blob/master/Common/ConnectAsyncExtension.Net40.cs

f:\codeforgitblit\supersocket.clientengine\common\connectasyncextension.net40.cs

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]);
            }
            else
            {
                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)
                m_ConnectMethod.Invoke(null, 
                    new object[]
                    {
                        SocketType.Stream,
                        ProtocolType.Tcp,
                        CreateSocketAsyncEventArgs(remoteEndPoint, callback, state)
                    });
            else
            {
                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);
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用C++的DLL搜集整理的所有数据类型转换方式发布时间:2022-07-14
下一篇:
C# CRC8的实现(原创)发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap