日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#静态构造函数

發布時間:2024/4/17 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#静态构造函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

私有構造函數
私有構造函數是一種特殊的實例構造函數。它通常用在只包含靜態成員的類中。如果類具有一個或多個私有構造函數而沒有公共構造函數,則其他類(除嵌套類外)無法創建該類的實例。例如:

class NLog {// Private Constructor:private NLog() { }public static double e = Math.E; //2.71828... }

?

聲明空構造函數可阻止自動生成默認構造函數。注意,如果您不對構造函數使用訪問修飾符,則在默認情況下它仍為私有構造函數。但是,通常顯式地使用 private 修飾符來清楚地表明該類不能被實例化。
當沒有實例字段或實例方法(如 Math 類)時或者當調用方法以獲得類的實例時,私有構造函數可用于阻止創建類的實例。如果類中的所有方法都是靜態的,可考慮使整個類成為靜態的。

下面是使用私有構造函數的類的示例。

public class Counter {private Counter() { }public static int currentCount;public static int IncrementCount(){return ++currentCount;} }class TestCounter {static void Main(){// If you uncomment the following statement, it will generate// an error because the constructor is inaccessible:// Counter aCounter = new Counter(); // Error Counter.currentCount = 100;Counter.IncrementCount();Console.WriteLine("New count: {0}", Counter.currentCount);// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();} }

?

?

輸出:

New count: 101

?

注意,如果您取消注釋該示例中的以下語句,它將生成一個錯誤,因為該構造函數受其保護級別的限制而不可訪問:

// Counter aCounter = new Counter(); // Error

靜態構造函數
靜態構造函數用于初始化任何靜態數據,或用于執行僅需執行一次的特定操作。在創建第一個實例或引用任何靜態成員之前,將自動調用靜態構造函數。

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;} }

?

靜態構造函數具有以下特點:

  • 靜態構造函數既沒有訪問修飾符,也沒有參數。
  • 在創建第一個實例或引用任何靜態成員之前,將自動調用靜態構造函數來初始化類。
  • 無法直接調用靜態構造函數。
  • 在程序中,用戶無法控制何時執行靜態構造函數。

靜態構造函數的典型用途是:當類使用日志文件時,將使用這種構造函數向日志文件中寫入項。

靜態構造函數在為非托管代碼創建包裝類時也很有用,此時該構造函數可以調用 LoadLibrary 方法。
如果靜態構造函數引發異常,運行時將不會再次調用該構造函數,并且在程序運行所在的應用程序域的生存期內,類型將保持未初始化。
在此示例中,類 Bus 有一個靜態構造函數。創建 Bus 的第一個實例(bus1)時,將調用該靜態構造函數來初始化該類。輸出示例驗證了即使創建 Bus 的兩個實例,該靜態構造函數也僅運行一次,并且在實例構造函數運行之前運行。

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();} }

輸出:

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.

?

?

總結

以上是生活随笔為你收集整理的C#静态构造函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。