偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#靜態(tài)構(gòu)造函數(shù)特點(diǎn)淺析

開發(fā) 后端
C#靜態(tài)構(gòu)造函數(shù)的特點(diǎn)是什么呢?C#靜態(tài)構(gòu)造函數(shù)是如何工作的呢?那么本文就向你介紹這方面的內(nèi)容。

C#靜態(tài)構(gòu)造函數(shù)的特點(diǎn)是什么呢?讓我們首先來了解下C#靜態(tài)構(gòu)造函數(shù):

C#靜態(tài)構(gòu)造函數(shù)用于初始化任何靜態(tài)數(shù)據(jù),或用于執(zhí)行僅需執(zhí)行一次的特定操作。在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)。

  1. class SimpleClass{    // Static constructor      
  2.  
  3. static SimpleClass()    {        //...    }} 

C#靜態(tài)構(gòu)造函數(shù)具有以下特點(diǎn):

C#靜態(tài)構(gòu)造函數(shù)既沒有訪問修飾符,也沒有參數(shù)。

在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)來初始化類。

無法直接調(diào)用靜態(tài)構(gòu)造函數(shù)。

在程序中,用戶無法控制何時(shí)執(zhí)行靜態(tài)構(gòu)造函數(shù)。

C#靜態(tài)構(gòu)造函數(shù)的典型用途是:當(dāng)類使用日志文件時(shí),將使用這種構(gòu)造函數(shù)向日志文件中寫入項(xiàng)。

C#靜態(tài)構(gòu)造函數(shù)在為非托管代碼創(chuàng)建包裝類時(shí)也很有用,此時(shí)該構(gòu)造函數(shù)可以調(diào)用 LoadLibrary 方法。

C#靜態(tài)構(gòu)造函數(shù)示例

在此示例中,類 Bus 有一個(gè)靜態(tài)構(gòu)造函數(shù)和一個(gè)靜態(tài)成員 Drive()。當(dāng)調(diào)用 Drive() 時(shí),將調(diào)用靜態(tài)構(gòu)造函數(shù)來初始化類

  1. public class Bus{     
  2.  // Static constructor:    
  3.   static Bus()    {       
  4.    System.Console.WriteLine(  
  5. "The static constructor invoked.");   
  6.    }     
  7.  public static void Drive()    {        System.Console.WriteLine(  
  8. "The Drive method invoked.");    
  9.   }  
  10. }  
  11. class TestBus{    
  12.   static void Main()    {   
  13.       Bus.Drive();    
  14.   }  

C#靜態(tài)構(gòu)造函數(shù)示例之輸出:

  1. The static constructor invoked.   
  2.  
  3. The Drive method invoked.  

C#靜態(tài)構(gòu)造函數(shù)的特點(diǎn)就向你介紹到這里,希望對你學(xué)習(xí)了解C#靜態(tài)構(gòu)造函數(shù)有所幫助。

【編輯推薦】

  1. C#指針使用簡析
  2. C#結(jié)構(gòu)學(xué)習(xí)的詳細(xì)介紹
  3. 關(guān)于C#結(jié)構(gòu)的一些認(rèn)識
  4. C#構(gòu)造函數(shù)與C++的區(qū)別淺析
  5. C#構(gòu)造函數(shù)的運(yùn)用淺析
責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-07-31 15:44:02

C#靜態(tài)構(gòu)造函數(shù)

2009-08-13 18:10:31

C#靜態(tài)構(gòu)造函數(shù)

2009-08-14 09:15:28

C#調(diào)用構(gòu)造函數(shù)

2009-08-20 14:28:00

C#靜態(tài)構(gòu)造函數(shù)

2009-07-31 15:37:45

C#靜態(tài)構(gòu)造函數(shù)

2009-09-18 09:02:45

CLR Via C#

2009-08-13 18:15:06

C#繼承構(gòu)造函數(shù)

2009-08-13 17:38:42

C#構(gòu)造函數(shù)

2009-11-17 09:07:55

靜態(tài)構(gòu)造函數(shù)

2011-06-11 21:36:44

C#C++

2009-08-14 09:58:09

C#復(fù)制構(gòu)造函數(shù)

2009-08-13 13:42:54

C#構(gòu)造函數(shù)

2009-08-14 09:43:59

C#復(fù)制構(gòu)造函數(shù)

2009-08-25 17:59:49

C#入門

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2009-08-13 18:36:36

C#繼承構(gòu)造函數(shù)

2009-08-14 09:50:46

C#復(fù)制構(gòu)造函數(shù)

2009-08-13 11:18:50

C#結(jié)構(gòu)體

2009-08-28 11:48:32

C#靜態(tài)函數(shù)

2009-07-31 14:03:21

C# Format函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號